我的程序与充当服务器的嵌入式蓝牙设备连接。我成功找到了附近的蓝牙设备,但是当我尝试连接到新设备时,我的程序在调用 BluetoothSocket.connect() 时由于 IO 异常而崩溃。我无法弄清楚发生了什么,所以如果有人可以帮助我,我会非常感激。
我认为这可能与我随机生成的 UUID 有关,但我并不完全确定。
谢谢。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnScanDevice = (Button) findViewById( R.id.scandevice );
stateBluetooth = (TextView) findViewById( R.id.bluetoothstate );
startBluetooth();
listDevicesFound = (ListView) findViewById( R.id.devicesfound );
btArrayAdapter = new ArrayAdapter<String>( AndroidBluetooth.this,
android.R.layout.simple_list_item_1 );
listDevicesFound.setAdapter( btArrayAdapter );
CheckBlueToothState();
btnScanDevice.setOnClickListener( btnScanDeviceOnClickListener );
registerReceiver( ActionFoundReceiver, new IntentFilter( BluetoothDevice.ACTION_FOUND ) );
listDevicesFound.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
myBtDevice = btDevicesFound.get( arg2 );
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
iStream = btSocket.getInputStream();
oStream = btSocket.getOutputStream();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer One" );
}
myBtAdapter.cancelDiscovery();
CheckBlueToothState();
try {
btSocket.connect();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "IO Exception" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer Two" );
}
}
});
}
private void CheckBlueToothState() {
if( myBtAdapter == null ) {
stateBluetooth.setText("Bluetooth NOT supported" );
} else {
if( myBtAdapter.isEnabled() ) {
if( myBtAdapter.isDiscovering() ) {
stateBluetooth.setText( "Bluetooth is currently " +
"in device discovery process." );
} else {
stateBluetooth.setText( "Bluetooth is Enabled." );
btnScanDevice.setEnabled( true );
}
} else {
stateBluetooth.setText( "Bluetooth is NOT enabled" );
Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
}
}
}
private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() {
public void onClick( View arg0 ) {
btArrayAdapter.clear();
myBtAdapter.startDiscovery();
}
};
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
if( requestCode == REQUEST_ENABLE_BT ) {
CheckBlueToothState();
}
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
public void onReceive( Context context, Intent intent ) {
String action = intent.getAction();
if( BluetoothDevice.ACTION_FOUND.equals( action ) ) {
BluetoothDevice btDevice = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
btDevicesFound.add( btDevice );
btArrayAdapter.add( btDevice.getName() + "\n" + btDevice.getAddress() );
btArrayAdapter.notifyDataSetChanged();
}
}
};
public static void startBluetooth(){
try {
myBtAdapter = BluetoothAdapter.getDefaultAdapter();
myBtAdapter.enable();
} catch ( NullPointerException ex ) {
Log.e( "Bluetooth", "Device not available" );
}
}
public static void stopBluetooth() {
myBtAdapter.disable();
}