как подключить сопряженное устройство с помощью средства выбора устройства - PullRequest
0 голосов
/ 01 августа 2020
public class BluetoothReceiver {

  public BluetoothReceiver(Context context) {
    BroadcastReceiver receiver = new BlueToothConnectBroadcastReceiver();
    context.registerReceiver(receiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED));

    Intent i = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
    i.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
    i.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL);
    i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    context.startActivity(i);
    }


}

class BlueToothConnectBroadcastReceiver extends BroadcastReceiver {


@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceive(Context context, Intent intent) {


    if (BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) {
        context.unregisterReceiver(this);
        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        System.out.println("device: "+device.getName() + "  bonded? : " + device.getBondState());


        if (device.getBondState() == 12 /*BluetoothDevice.BOND_BONDED*/) {
            device.connectGatt(context, true, new BluetoothGattCallback() {
            }, BluetoothDevice.TRANSPORT_AUTO);
        }
     



         }
     }
}

вывод: I / System.out: устройство: BH104 связано? : 12

D / BluetoothGatt: connect () - устройство: 77: A6: 03: 70: 82: F9, автоматически: true registerApp ()

D / BluetoothGatt: registerApp () - UUID = 14ebef2b-bbf5-4b6 c -9a38-3d4147e4d1c2

соединение не изменено, устройство, спасибо за ответы

...