使用Android 4.3/Samsung BLE 2.0 SDK时,观察到当外围设备关闭时,SDK将立即或在延迟20秒后接收onConnectionStateChange(DEVICE_DISCONNECTED).根据我的经验,这取决于外围设备实现,其中一些将尝试报告它们被关闭而有些则没有,因此SDK必须等待约20秒才能超时.
为了消除这种行为,我尝试使用Timer来检查我是否可以读取某个特征.如果读取超时,我会调用disconnect(Android 4.3)/ cancelConnection(Samsung)来终止连接.调用本身成功,onConnectionStateChange回调返回状态GATT_SUCCESS.然后我打开外围设备并立即连接到它,发现服务,并在我尝试读/写/通知任何通知时遇到问题.通过在iOS中使用LightBlue,我可以确认外围设备没有连接.
关闭外围设备20秒后,我将收到DEVICE_DISCONNECTED回调.之后我再次联系,一切运作得很好.
有两个问题:1.我们是否应该在20秒延迟期间连接到外围设备?2.外围设备关闭时是否有任何方法可以获得通知?
提前致谢.
我的Android手机允许我重命名我配对的设备,方法是转到[设置>无线和网络>蓝牙]活动页面,然后点击配对蓝牙设备右侧的设置按钮.但是,当我使用BluetoothAdapter.getBondedDevices()函数查询Bonded设备列表时,结果中显示的名称是设备的默认名称.
如何访问蓝牙设备的重命名名称?
我的目标是找到附近的蓝牙设备(LE设备和"经典"),以便将当前可见的附近设备与我的应用程序使用它的某些功能相关联. (不是特定的设备/设备,而是所有设备/设备!!!)
我知道的:
startLeScan() 只能用BLE设备回调BluetoothDevice通过广播返回找到的. 我不确定的是:
startScan()将始终发现可发现的BLE和经典设备. BluetoothDevice.connectGatt() 添加了新的BLE API,但也应该使用经典蓝牙(返回GATT服务......).我想知道的是:
startScan()返回两种类型(Classic和BLE),那么在电池消耗,性能,良好实践和其他方面使用哪种更好?我的应用程序将定期执行后台扫描,因此我希望尽可能减少对电池消耗的影响.
我正在关注Bluetooth Low Energy设备的文档以扫描BLE设备.
正如文档中所提到的,我定义了---
BluetoothAdapter mBluetoothAdapter = null;
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter(); //Lint Error..
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个Lint错误---
调用需要API级别18(当前最小值为8):android.bluetooth.BluetoothManager#getAdapter
所以我改变了我的代码 -
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)
代码是否替换上述lint错误?
android 文档说明:
Note: You can only scan for Bluetooth LE devices or scan for Classic Bluetooth devices, as described in Bluetooth. You cannot scan for both Bluetooth LE and classic devices at the same time.
Run Code Online (Sandbox Code Playgroud)
但是我注意到调用mBtAdapter.startDiscovery(); 正在返回经典设备和btle设备.有人知道这里有什么问题吗?
我有一个运行GATT服务器的嵌入式系统,我试图通过Android连接到该服务器.任务很简单:连接到GATT服务器,导航特征并验证读/写功能.问题是,当我尝试连接Android应用时,它会尝试与"Passkey"配对,而不是与"Just Works"配对[ https://developer.bluetooth.org/TechnologyOverview/Pages/LE-Security.aspx].这是不可接受的,因为我的嵌入式设备没有任何方法向用户显示用于配对的密钥.所以连接方法必须是Just Works.
我在Play商店尝试了很多应用程序,所有这些应用程序都只尝试与Passkey配对.但这是我正在使用的当前版本:https://play.google.com/store/apps/details?id = com.macdom.ble.blescanner
我使用LightBlue应用程序(OS X和iOS支持).所以我知道我的外围设备(GATT服务器)正在工作:广告,可连接,特征可访问等.
如何强制Android使用Just Works而不是Passkey配对?我缺少外设配置吗?
谢谢
我没有找到任何关于Bluetooth 3.0 HS支持的信息Android.那有什么API用吗?它是否受到支持?
我有一个Android-based (4.1.2 with BlueZ stack)硬件支持的设备Bluetooth 3.0 HS.我正在尝试调查需要多少努力才能添加Bluetooth 3.0 HS对Android+的支持BlueZ.可能是更好的切换到更新Android和Bluetooth stack (bluedroid)?
我正在使用设备名称过滤器连接到特定的 BLE 设备。但是我的扫描回调没有执行。当我使用 MAC 地址作为过滤器时,它工作正常。这是已知问题还是错误?我正在使用 Android 6.0 的华硕平板电脑进行测试。当我从设备名称数据库读取要使用的扫描过滤器时,我没有使用 MAC 地址的替代方法。
//这不起作用
ScanFilter scanFilter = (new android.bluetooth.le.ScanFilter.Builder())
.setDeviceName(device_name).build();
Run Code Online (Sandbox Code Playgroud)
//这有效
ScanFilter scanFilter = new ScanFilter.Builder()
.setDeviceAddress(dev_mac_address).build();
Run Code Online (Sandbox Code Playgroud) 我有一个蓝牙服务器,它使用 bleno 并向客户端返回可用 Wifi 网络的列表。的代码readCharacteristic看起来基本上是这样的:
class ReadCharacteristic extends bleno.Characteristic {
constructor(uuid, name, action) {
super({
uuid: uuid,
properties: ["read"],
value: null,
descriptors: [
new bleno.Descriptor({
uuid: "2901",
value: name
})
]
});
this.actionFunction = action;
}
onReadRequest(offset, callback) {
console.log("Offset: " + offset);
if(offset === 0) {
const result = this.actionFunction();
result.then(value => {
this.actionFunctionResult = value;
const data = new Buffer.from(value).slice(0,bleno.mtu);
console.log("onReadRequest: " + data.toString('utf-8'));
callback(this.RESULT_SUCCESS, data);
}, err => {
console.log("onReadRequest error: " + err);
callback(this.RESULT_UNLIKELY_ERROR); …Run Code Online (Sandbox Code Playgroud) javascript android bluetooth-lowenergy android-bluetooth bleno
我在Android应用程序中创建了一个蓝牙活动类,它适用于所有蓝牙功能,如:扫描,配对,连接,发送和接收数据.
真正的问题是当Activity被销毁时.蓝牙正在断开连接.
如何在整个应用程序中建立蓝牙连接.我想从其他活动向蓝牙发送数据.
帮助我以简单的方式实现这一点?
public class BTActivity extends AppCompatActivity {
ArrayList<BluetoothDevice> devices = new ArrayList<>();
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mBluetoothDevice;
ArrayAdapter<BluetoothDevice> arrayAdapter;
ConnectThread c;
private static final String TAG = "MY_BT";
ListView lvPaired;
Button BluetoothOnOff, ScanBt, pairedlist, sendButton, btDisconnect, incrementBtn, decrementBtn;
EditText input_text;
TextView ConnectedTo;
FragmentManager fm = getSupportFragmentManager();
UUID BHANU_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
lvPaired = findViewById(R.id.btPairedLV);
pairedlist = findViewById(R.id.btPaired);
input_text = findViewById(R.id.user_input);
sendButton = findViewById(R.id.btnSend);
BluetoothOnOff = findViewById(R.id.offOn_bt);
ScanBt = …Run Code Online (Sandbox Code Playgroud) service android bluetooth background-service android-bluetooth