android - 识别蓝牙连接
问题描述
我想在连接特定设备 BT 时启动应用程序。
我不想使用 IFTTT,我想编写自己的应用程序,能够自主实现 IFTTT 对任何应用程序所做的事情:在设备 xxx 上的蓝牙连接事件完成时启动。
我怎样才能做到这一点?
解决方案
首先,您需要检测智能手机已连接到 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);
}
}
推荐阅读
- ios - 如何在 infoplist.strings 文件中使用用户定义的设置?
- sql-server - SQLSTATE[42000]:[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]'FIELD' 不是可识别的内置函数名
- r - 将 week+day(39+3) 变量更改为 R 中的周或天
- java - 在代码中编写此公式是否有更清洁/更简单的方法?
- django - Django 服务器找不到静态文件
- angular - 来自 web api 的数据未绑定到 Angular 中的视图
- sql - 使用 SQL 将两个相似数据的查询编译到一个视图中
- python - 在 Python 数据框中获取部分时间(早上、下午、晚上、晚上)
- django - TinyMCE JS 在 django 项目中返回 404
- javascript - 如何通过 grid-auto-flow:column 使自动放置的 div 相互粘连?