搭建直播带货平台,Android蓝牙连接状态检测
程序员文章站
2022-06-14 23:08:51
...
搭建直播带货平台,Android蓝牙连接状态检测的相关代码
private void checkConnectState() {
ArrayList<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
try {
//得到连接状态的方法
Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
//打开权限
method.setAccessible(true);
int state = (Integer) method.invoke(adapter, (Object[]) null);
if (state == BluetoothAdapter.STATE_CONNECTED) {
Log.i("BLUETOOTH", "BluetoothAdapter.STATE_CONNECTED");
Set<BluetoothDevice> devices = adapter.getBondedDevices();
Log.i("BLUETOOTH", "devices:" + devices.size());
for (BluetoothDevice device : devices) {
Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
method.setAccessible(true);
boolean isConnected = (Boolean) isConnectedMethod.invoke(device, (Object[]) null);
if (isConnected) {
Log.i("BLUETOOTH", "connected:" + device.getName());
deviceList.add(device);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是 搭建直播带货平台,Android蓝牙连接状态检测的相关代码,更多内容欢迎关注之后的文章
上一篇: zendstudio文件编码修改问题