Android BluetoothSocket.connect()引发IOException
我的程序与作为服务器的嵌入式蓝牙设备连接。我成功地找到了附近的蓝牙设备,但当我尝试连接到新设备时,我的程序在调用BluetoothSocket.connect()时由于IO异常而崩溃。我很难弄清楚到底发生了什么,所以如果有人能帮我,我将非常感激 我认为这可能与我随机生成的UUID有关,但我不能完全确定 谢谢Android BluetoothSocket.connect()引发IOException,android,exception,io,bluetooth,Android,Exception,Io,Bluetooth,我的程序与作为服务器的嵌入式蓝牙设备连接。我成功地找到了附近的蓝牙设备,但当我尝试连接到新设备时,我的程序在调用BluetoothSocket.connect()时由于IO异常而崩溃。我很难弄清楚到底发生了什么,所以如果有人能帮我,我将非常感激 我认为这可能与我随机生成的UUID有关,但我不能完全确定 谢谢 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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();
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnScanDevice=(按钮)findViewById(R.id.scandevice);
stateBluetooth=(TextView)findViewById(R.id.bluetoothstate);
startBluetooth();
listDevicesFound=(ListView)findViewById(R.id.devicesfound);
btArrayAdapter=新的ArrayAdapter(AndroidBluetooth.this,
android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
CheckBlueToothState();
btnScanDevice.setOnClickListener(btnscandeviceonConClickListener);
registerReceiver(ActionFoundReceiver,新的IntentFilter(BluetoothDevice.ACTION_-FOUND));
listDevicesFound.setOnItemClickListener(新的OnItemClickListener()
{
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3)
{
myBtDevice=btDevicesFound.get(arg2);
试一试{
btSocket=myBtDevice.createrFComsockettoServiceRecord(我的UUID);
iStream=btSocket.getInputStream();
oStream=btSocket.getOutputStream();
}捕获(IOE异常){
Log.e(“蓝牙套接字”、“蓝牙不可用或权限不足”);
}捕获(NullPointerException e){
Log.e(“蓝牙套接字”、“空指针一”);
}
myBtAdapter.cancelDiscovery();
CheckBlueToothState();
试一试{
btSocket.connect();
}捕获(IOE异常){
Log.e(“蓝牙套接字”、“IO异常”);
}捕获(NullPointerException e){
Log.e(“蓝牙套接字”、“空指针二”);
}
}
});
}
私有void CheckBlueToothState(){
if(myBtAdapter==null){
stateBluetooth.setText(“不支持蓝牙”);
}否则{
if(myBtAdapter.isEnabled()){
if(myBtAdapter.isDiscovering()){
stateBluetooth.setText(“蓝牙当前正在运行”+
“在设备发现过程中。”);
}否则{
stateBluetooth.setText(“蓝牙已启用”);
btnScanDevice.setEnabled(true);
}
}否则{
stateBluetooth.setText(“蓝牙未启用”);
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
}
}
private Button.OnClickListener BTNSCandeviceonConClickListener=新建按钮。OnClickListener(){
公共void onClick(视图arg0){
btArrayAdapter.clear();
myBtAdapter.startDiscovery();
}
};
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==请求启用){
CheckBlueToothState();
}
}
private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
BluetoothDevice btDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
btDevicesFound.add(btDevice);
添加(btDevice.getName()+“\n”+btDevice.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};
公共静态无效startBluetooth(){
试一试{
myBtAdapter=BluetoothAdapter.getDefaultAdapter();
myBtAdapter.enable();
}捕获(NullPointerException ex){
Log.e(“蓝牙”,“设备不可用”);
}
}
公共静态void stopBluetooth(){
myBtAdapter.disable();
}
您发布的代码有两个问题,尽管只有一个问题与您的崩溃有关
最有可能的是,您在logcat中的崩溃显示类似“命令被拒绝”的内容。UUID是一个必须指向嵌入式设备上已发布服务的值,它不能只是随机生成。换句话说,您要访问的RFCOMM SPP连接有一个特定的UUID,它发布该UUID以标识该服务,并且当您创建套接字时,它必须使用匹配的UUID
可以帮助您查询设备以获得需要插入程序的正确UUID。在Android 4.0之前,SDK几乎假设您提前知道它(您从蓝牙OEM等处获得),因此从您的设备上发现它有点迂回。如果您有幸拥有4.0.3设备,fetchUuidsWithSdp()
和getUuids()
现在是公共方法,您可以直接调用它们来查找所有已发布的服务及其关联的UUID值
代码的第二个问题可能会在稍后出现,即在连接之前,您无法从套接字获取数据流,因此您可能需要像下面这样重写您的方法:
myBtDevice = btDevicesFound.get( arg2 );
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
}
myBtAdapter.cancelDiscovery();
CheckBlueToothState();
try {
btSocket.connect();
//Get streams after connect() returns without error
iStream = btSocket.getInputStream();
oStream = btSocket.getOutputStream();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "IO Exception" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer Two" );
}
HTH您发布的代码有两个问题,尽管只有一个与您的崩溃有关 最有可能的是,您在logcat中的崩溃显示类似“命令被拒绝”的内容。UUID是一个必须指向嵌入式设备上已发布服务的值,它不能只是随机生成。换句话说,您要访问的RFCOMM SPP连接有一个特定的UUID