Programmatically connect to paired Bluetooth device

前端 未结 4 548
眼角桃花
眼角桃花 2020-11-27 11:14

Is there a way, using the Android SDK, to programmatically connect to an already-paired Bluetooth device?

In other words: I can go into Settings -> Wireless & ne

相关标签:
4条回答
  • 2020-11-27 11:35

    the best way I found to solve my problem was finding out that I can create a button that brings up the Bluetooth Settings screen. I didn't realize you could do this, or I would have from the beginning.

    startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
    
    0 讨论(0)
  • 2020-11-27 11:41

    if the device is already paired , then you can use

    if(device.getBondState()==device.BOND_BONDED){
    
            Log.d(TAG,device.getName());
            //BluetoothSocket mSocket=null;
            try {
    
    
                mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                Log.d(TAG,"socket not created");
                e1.printStackTrace();
            }
            try{
    
                mSocket.connect();
    
            }
            catch(IOException e){
                try {
    
                    mSocket.close();
                    Log.d(TAG,"Cannot connect");
                } catch (IOException e1) {
                    Log.d(TAG,"Socket not closed");
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    
    
            }
    

    for the MY_UUID use

    private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
    

    the above code snippet is just to connect your device to an A2DP supported device. I hope it will work.

    0 讨论(0)
  • 2020-11-27 11:41

    I used the code here as a starting point for this functionality in my app: http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices

    Once the device is paired, the app has no problem connecting the two devices together programmtically.

    0 讨论(0)
  • 2020-11-27 11:42

    Okay, since this was driving me crazy, I did some digging into the source code and I've found a 100% reliable (at least on my Nexus 4, Android 4.3) solution to connect to a paired A2DP device (such as a headset or Bluetooth audio device). I've published a fully working sample project (easily built with Android Studio) that you can find here on Github.

    Essentially, what you need to do is:

    • Get an instance of the BluetoothAdapter
    • Using this instance, get a profile proxy for A2DP:

    adapter.getProfileProxy (context, listener, BluetoothProfile.A2DP);

    where listener is a ServiceListener that will receive a BluetoothProfile in its onServiceConnected() callback (which can be cast to a BluetoothA2dp instance)

    • Use reflection to acquire the connect(BluetoothDevice) method on the proxy:

    Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);

    • Find your BluetoothDevice:

    String deviceName = "My_Device_Name";
    
    BluetoothDevice result = null;
    
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    if (devices != null) {
        for (BluetoothDevice device : devices) {
            if (deviceName.equals(device.getName())) {
                result = device;
                break;
            }
        }
    }
    

    • And invoke the connect() method:

    connect.invoke(proxy, result);

    Which, at least for me, caused an immediate connection of the device.

    0 讨论(0)
提交回复
热议问题