首页 > 解决方案 > 识别蓝牙连接

问题描述

我想在连接特定设备 BT 时启动应用程序。

我不想使用 IFTTT,我想编写自己的应用程序,能够自主实现 IFTTT 对任何应用程序所做的事情:在设备 xxx 上的蓝牙连接事件完成时启动。

我怎样才能做到这一点?

标签: androidbluetoothandroid-bluetooth

解决方案


首先,您需要检测智能手机已连接到 BLE 设备。您可以在BluetoothGattCallback's中onConnectionStateChange()通过检查是否newState等于 来做到这一点BluetoothGatt.STATE_CONNECTED

然后,启动目标应用程序的活动:

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    if (newState == BluetoothGatt.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
        // start your target application's activity here
        Intent intent = new Intent(com.yourapp.ACTION);
        startActivity(intent);
    }
}

推荐阅读