2012-06-11 194 views
1

我的程序与作为所述服务器的嵌入式蓝牙设备连接。我成功找到附近的蓝牙设备,但是当我尝试连接到新设备时,当调用BluetoothSocket.connect()时,由于IO异常,我的程序崩溃。我无法弄清楚发生了什么事情,所以如果有人能帮助我,我会非常感激。BluetoothSocket.connect()抛出IOException异常

我认为它可能有一些做的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(); 
} 

回答

3

您发布的代码有两个问题,尽管只有一个与您的崩溃有关。

最有可能你在logcat的崩溃类似于“命令被拒绝”。该UUID是必须指向一个发布的服务您的嵌入式设备,它不能仅仅是随机生成的上的值。换句话说,你希望RFCOMM SPP连接来访问有其发布到识别的服​​务,当你创建一个套接字,必须使用匹配的UUID特定的UUID。

This blog post I wrote可以帮助您如何查询您的设备以获取需要被插入到你的程序的正确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

+0

非常感谢。不幸的是,我正在为此应用程序使用Android 2.0。我原本以为它会是一个特定的UUID,但无法在任何地方找到它。如果你知道在哪里看,或如何看,我会很感激。该应用程序运行在联想Ideapad A1上,并试图连接到嵌入式RN-41蓝牙芯片。它也不能连接到只有一个芯片,它应该有能力连接到几个不同的芯片(不是同时,但你可以断开连接并找到另一个芯片)。再次感谢 – JuiCe

+1

所以...阅读博文,它会告诉你如何在2.0 – Devunwired

+0

上做到这一点哈哈抱歉,我点击链接之前发布,但我一直在阅读它。非常感谢您的帮助 – JuiCe