- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写扫描 BLE 设备的 android 应用程序,当它找到具有命名方案的某些设备时,它会连接到它并读取一个特征(用户定义的设备名称),然后立即断开连接。然后它将在列表中显示该设备以及找到的任何其他设备并读取用户定义的名称。然后,用户可以选择要连接的设备(或多个设备)并连接到它并从中流式传输数据。
不断发生的问题是在它获得用户定义的名称并断开连接后,BLE 设备停止广播并且我在扫描时无法再找到它,或者如果我在读取用户定义的名称后尝试连接它并且断开连接。
这是 Android BLE 堆栈的问题还是我需要添加更多延迟(我在使用的整个蓝牙服务中有 100 毫秒的延迟)
这是我在服务中使用的部分代码
public boolean initialize() {
Log.i(TAG, "Initializing");
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
if (mBluetoothManager == null) {
Log.e(TAG, "Unable to initialize BluetoothManager.");
return false;
}
}
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
return false;
}
mReadyToWrite = true;
mReadyToRead = true;
mReady = true;
mCharacteristicWriteQueue = new ArrayDeque<BluetoothGattCharacteristic>();
mCharacteristicReadQueue = new ArrayDeque<BluetoothGattCharacteristic>();
mDescriptorWriteQueue = new ArrayDeque<BluetoothGattDescriptor>();
mDescriptorReadQueue = new ArrayDeque<BluetoothGattDescriptor>();
//mBluetoothGattMap = new HashMap<String, BluetoothGatt>();
return true;
}
/**
* Connects to the GATT server hosted on the Bluetooth LE device.
*
* @param address The device address of the device.
*
* @return Return true if the connection is initiated successfully. The connection result
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
*/
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
if(mBluetoothGattMap.containsKey(address)) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGattMap.get(address).connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
mBluetoothGattMap.put(address, device.connectGatt(this, false, mGattCallback));
Log.d(TAG, "Trying to create a new connection to address " + address);
//mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
/**
* Disconnects an existing connection or cancel a pending connection. The disconnection result
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
*/
public void disconnect(String address) {
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
Log.i(TAG, "Disconnecting from gatt");
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
mBluetoothGattMap.get(address).disconnect();
}
public void close(String address) {
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
mBluetoothGattMap.get(address).close();
mBluetoothGattMap.remove(address);
Log.w(TAG, "Succeeed removing it");
}
public int getConnectionState(String address) {
Log.i(TAG, "getting connection state for " + address);
BluetoothGatt gatt = mBluetoothGattMap.get(address);
return mBluetoothManager.getConnectionState(gatt.getDevice(), BluetoothProfile.GATT);
}
/**
* Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
* asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
* callback.
*
* @param characteristic The characteristic to read from.
*/
public void readCharacteristic(String address, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "reading characteristic");
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
if(mReadyToRead && mReady) {
boolean result = mBluetoothGattMap.get(address).readCharacteristic(characteristic);
mReadyToRead = false;
mReady = false;
if(!result) {
Log.i(TAG, "read failed");
}
}else {
mCharacteristicReadQueue.push(characteristic);
}
}
public void writeCharacteristic(String address, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "writeCharacteristic - readyToWrite = " + mReadyToWrite + " queue size = " + mCharacteristicWriteQueue.size());
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
if(mReadyToWrite && mReady) {
boolean result = mBluetoothGattMap.get(address).writeCharacteristic(characteristic);
mReadyToWrite = false;
mReady = false;
if(!result) {
Log.i(TAG, "characteristic write failed");
}
}else {
mCharacteristicWriteQueue.push(characteristic);
}
}
public void readDescriptor(String address, BluetoothGattDescriptor descriptor) {
Log.i(TAG, "reading descriptor");
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
if(mReadyToRead && mReady) {
boolean result = mBluetoothGattMap.get(address).readDescriptor(descriptor);
mReadyToRead = false;
mReady = false;
if(!result) {
Log.i(TAG, "descriptor read failed");
}
}else {
mDescriptorReadQueue.push(descriptor);
}
}
public void writeDescriptor(String address, BluetoothGattDescriptor descriptor) {
Log.i(TAG, "writing descriptor for characteristic " + descriptor.getCharacteristic().getUuid().toString());
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
if(mReadyToWrite && mReady) {
boolean result = mBluetoothGattMap.get(address).writeDescriptor(descriptor);
mReadyToWrite = false;
mReady = false;
if(!result) {
Log.i(TAG, "descriptor write failed");
}
}else {
mDescriptorWriteQueue.push(descriptor);
}
}
public BluetoothGattCharacteristic getCharacteristic(String address, UUID uuid) {
if(!mBluetoothGattMap.containsKey(address)) {
Log.i(TAG, "Device address " + address + " not found");
return null;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
for(BluetoothGattService service : mBluetoothGattMap.get(address).getServices()) {
Log.i(TAG, "Service: " + service.getUuid().toString());
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
Log.i(TAG, "Characteristic: " + characteristic.getUuid().toString());
if(characteristic.getUuid().equals(uuid)) {
return characteristic;
}
}
}
Log.i(TAG, "Characteristic not found");
return null;
}
public Set<String> getConnectedDevices(){
return this.mBluetoothGattMap.keySet();
}
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(String address, BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || !mBluetoothGattMap.containsKey(address)) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(100);
}
}catch(InterruptedException e){
//ignore
}
mBluetoothGattMap.get(address).setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHAR_CONFIG));
if(descriptor != null) {
boolean status = descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Log.i(TAG, "descriptor " + descriptor.getUuid().toString() + " setValue() status: " + status);
Log.i(TAG, "descriptor value: " + descriptor.getValue());
writeDescriptor(address, descriptor);
}
}
public void setPhoneEvents(byte priorities) {
for(String address : mBluetoothGattMap.keySet()) {
BluetoothGattCharacteristic characteristic = getCharacteristic(address, UUID.fromString(GattAttributes.ALERT_ATTRIBUTE));
if (characteristic != null) {
byte prioritiesBuf[] = new byte[1];
prioritiesBuf[0] = priorities;
characteristic.setValue(prioritiesBuf);
writeCharacteristic(address, characteristic);
Log.i(TAG, String.format("Forwarded phone alert priorities: 0x%X", priorities));
} else {
Log.e(TAG, "Failed to get the Alert ID characteristic from Gatt Server for device address " + address);
}
}
}
/**
* Retrieves a list of supported GATT services on the connected device. This should be
* invoked only after {@code BluetoothGatt#discoverServices()} completes successfully.
*
* @return A {@code List} of supported services.
*/
public List<BluetoothGattService> getSupportedGattServices(String address) {
if (mBluetoothGattMap.get(address) == null) return null;
return mBluetoothGattMap.get(address).getServices();
}
最佳答案
The problem that keeps happening is after it gets the user defined name and is disconnected the BLE devices stop broadcasting and I can no longer find it when I scan or if I try to connect to it after I read the user defined name and disconnected to it.
您的 BLE 设备不应该在连接断开后立即开始广播吗?我还建议您可以让您的 BLE 设备宣传您的自定义服务而不是您的应用程序以连接然后读取特性;你只需使用“ScanFilter”来过滤掉你最喜欢的设备。您只需让低级代码执行此操作即可。
关于Android BLE 连接和断开快速读取 1 个特征。一些BLE设备在快速连接断开后停止广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626499/
谁能给我提供代码或链接,以便在可能的情况下使用 UDP 发送和接收广播消息? 我一直被困在一个问题中,希望你们能帮助我解决它。谢谢 最佳答案 这是一个 C# 示例: using System; usi
我想将形状为 [a,b,c] 的张量中的元素相乘,每个元素在第 3 维中使用来自形状为 [a,b] 的张量的标量。 例如, x = |[1,2][3,4][5,6]| |[1,2][3,4][5,6]
广播是使具有不同形状的数组具有用于算术运算的兼容形状的过程。在 numpy 中,我们可以广播数组。 TensorFlow 图是否支持类似于 numpy 的广播? 最佳答案 是的,它是支持的。打开终端并
我有一个刷新功能,需要广播到子 Controller 。我在父 Controller 中做了类似的事情: // Refresh/Cancel $scope.OnGridBODRefre
我正在尝试在计算中使用字典值,如下所示: mydict = dict(zip(['key1', 'key2', 'key3'], [1, 2, 3])) print
刚刚掌握使用 MPI 的 Java 接口(interface)进行并行编程。只是想知道是否有人可以非常简单地解释广播的工作原理? 我有以下内容: if (me ==0) { // This is th
我正在处理一个项目,当我发送消息时,我将它作为通知发送给另一个用户使用广播它工作正常但是当我再次发送新消息然后替换为旧通知而不创建新通知 下面是我生成通知的代码 NotificationCompat.
我是 android 的初学者。但我非常需要你的帮助。我有一个流媒体视频广播视频项目。我找不到好的示例,在哪里可以实现从摄像机录制视频、将流发送(上传)到服务器以及从服务器下载(获取流)到播放器。请帮
请帮我解决我的问题。当我从父 Controller 调用并在子 Controller 中捕获时,为什么 $broadcast 函数不起作用?
我如何从 shell 中看到设置了哪些套接字选项?我特别想知道是否设置了 SO_BROADCAST? 最佳答案 你看过lsof了吗? 关于linux - 广播 socket ,我们在Stack Ove
当我在 Numpy 中进行此操作时会发生什么? a = np.ones([500,1]) b = np.ones([5000,])/2 c = a + b # a.shape (500,1) # b.
我有一个 Nexus S,当我在手机上手动更改日期时,并不总是广播 ACTION_DATE_CHANGED。如果我将日期从 2014 年 2 月 13 日更改为 2014 年 2 月 14 日,我还没
环境:springboot2.3.9RELEASE + RocketMQ4.8.0 依赖 <dependency>  
UDP 广播 面向连接的传输(如 TCP)管理两个网络端点之间的连接的建立,在连接的生命周期的有序和可靠的消息传输,以及最后,连接的有序终止。相比之下,类似 UDP 的无连接协议中则没有持久化连接的概
我正在开发一个带有 Angular 的单页应用程序,我需要在两个不同的指令之间进行通信,这些指令基本上没有父子关系。 在指令 A 中,我有 2 个地方需要从不同的功能广播相同的事件。在指令 B 中,为
我有一个带有多个重复项的主要二维 numpy 数组和一个具有第一个唯一值的辅助数组。 [[ 0 0 1 ] [ 1 0 2 ] [ 2 0 2 ] ... [ 0 0 1 ]
我正在制作多人网络游戏。现在要连接到服务器,客户端需要服务器的 ip 地址。 所以,我的实现方式如下。 客户端在广播 IP 和端口 A 上广播其 IP 地址。服务器通过 A 监听它,并且 服务器与客户
是否可以在没有 Urban Airship 等服务的情况下广播推送通知? 谢谢。 最佳答案 当然可以,但是您需要自己实现整个基础架构。 http://developer.apple.com/libra
我想复制矩阵的每一行 M没有任何复制发生(即通过创建 View ): 0 1 0 1 2 3 -> 0 1 2 3
我从一个 2D 数组开始,想将它广播到一个 3D 数组(例如,从灰度图像到 rgb 图像)。这是我使用的代码。 >>> img_grey = np.random.randn(4, 4) >>> img
我是一名优秀的程序员,十分优秀!