标签: android-bluetooth

java.lang.SecurityException:需要BLUETOOTH PRIVILEGED权限

有没有人遇到同样的问题,因为bluetoothDeive.createBond()用android 4.4 api 调用方法时显示以下错误消息?

java.lang.SecurityException:需要BLUETOOTH PRIVILEGED权限

注意:BLUETOOTH_ADMIN权限已包含在AndroidManifest文件中.

android-bluetooth

11
推荐指数
2
解决办法
1万
查看次数

Android BLE:onCharacteristicRead()似乎被线程阻止

我正在针对BLE设备实现一系列特征性读取.因为readCharacteristic()异步执行,并且因为我们必须等到它在发出另一个"读"调用之前完成,所以我使用了一个锁wait(),然后在'onCharacteristicRead()notify()的锁中再次进行操作.

当我wait()打电话后readCharacteristic(),我从来没有打过电话onCharacteristicRead().如果我不这样做wait(),那么我会接到电话onCharacteristicRead()并报告正确的值.

以下是阻止回调的相关代码onCharacteristicRead():

private void doRead() {
    //....internal accounting stuff up here....
    characteristic = mGatt.getService(mCurrServiceUUID).getCharacteristic(mCurrCharacteristicUUID);
    isReading = mGatt.readCharacteristic(characteristic);

    showToast("Is reading in progress? " + isReading);

    showToast("On thread: " + Thread.currentThread().getName());

    // Wait for read to complete before continuing.
    while (isReading) {
        synchronized (readLock) {
            try {
                readLock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public void onCharacteristicRead(BluetoothGatt …
Run Code Online (Sandbox Code Playgroud)

java multithreading android bluetooth-lowenergy android-bluetooth

11
推荐指数
1
解决办法
7934
查看次数

Android BLE:onCharacteristicChanged永远不会触发

我正在尝试编写一个Android应用程序,模仿我编写的iOS应用程序中已存在的功能.我正在与2个不同的BLE设备连接:

  1. 血压袖带
  2. 计重秤

在iOS上,我有两个设备都运行良好并报告数据.在Android上,我无法让它工作.经过数小时的研究和测试,我认为我要解决的基本问题是:

在iOS上,我调用以下代码以使BLE设备在有数据要报告时通知我的iOS设备:

#pragma mark - CBPeripheralDelegate Protocol methods
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    for (CBCharacteristic *characteristic in [service characteristics]) {
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }
}
Run Code Online (Sandbox Code Playgroud)

而已.iOS中此方法的注释说明如下:

如果指定的特性配置为允许通知和指示,则调用此方法仅启用通知.

基于此(以及在iOS中工作的事实),我正在计算我想要通知的特性的配置描述符应该像这样配置:

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
Run Code Online (Sandbox Code Playgroud)

考虑到这一点,我的BLEDevice班级看起来像这样:

public abstract class BLEDevice {
    protected BluetoothAdapter.LeScanCallback   mLeScanCallback;
    protected BluetoothGattCallback             mBluetoothGattCallback;
    protected byte[]                            mBytes;
    protected Context                           mContext;
    protected GotReadingCallback                mGotReadingCallback;
    protected String                            mDeviceName;
    public final static UUID                    UUID_WEIGHT_SCALE_SERVICE                       
            = UUID.fromString(GattAttributes.WEIGHT_SCALE_SERVICE);
    public final static UUID                    UUID_WEIGHT_SCALE_READING_CHARACTERISTIC        
            = UUID.fromString(GattAttributes.WEIGHT_SCALE_READING_CHARACTERISTIC); …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-bluetooth

11
推荐指数
1
解决办法
8148
查看次数

使用QR码作为带外(OOB)通道的蓝牙安全简单配对(SSP)

我有一个Windows 7应用程序,它使用Stollmann SDK成功地将PC与Android绑定.蓝牙MAC地址,哈希和随机化器的双向交换通过NFC 在带外进行:

应用qr代码

遗憾的是,Windows应用程序的源代码无法在此处共享.在Android端,一旦收到NDEF消息,就不需要应用程序,安全简单配对由操作系统(通过HandoverManager?)执行application/vnd.bluetooth.ep.oob.

现在我正在尝试创建一个Android应用程序,它将使用单向身份验证通过扫描的QR代码(而不是NFC)执行OOB配对.

自定义QR码将显示在PC屏幕上(由ZXing.Net生成)并包含蓝牙MAC地址,散列和随机发生器.

然而,在Android中似乎尚未实现OOB绑定 -

BluetoothAdapter.java:

/**
 * Read the local Out of Band Pairing Data
 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
 *
 * @return Pair<byte[], byte[]> of Hash and Randomizer
 *
 * @hide
 */
public Pair<byte[], byte[]> readOutOfBandData() {
    if (getState() != STATE_ON) return null;
    //TODO(BT
    /*
    try {
        byte[] hash;
        byte[] randomizer;

        byte[] ret = mService.readOutOfBandData(); …
Run Code Online (Sandbox Code Playgroud)

android qr-code bluetooth android-bluetooth bluetooth-oob

11
推荐指数
1
解决办法
3794
查看次数

预配对蓝牙设备

我希望能够预先配对蓝牙设备,以便在使用应用程序时节省用户混淆的步骤.我发现有用于预配对蓝牙设备的系统,方法和装置的专利 .

我正在寻找将一系列Android设备与一系列嵌入式设备配对,因此当设置Android设备时,我可以预先将它与需要与之通信的设备配对.我考虑过维护一个可以由应用程序下载和更新的MAC地址列表.

有没有办法配对两个设备而不必让它们接触?

android android-bluetooth pairing

11
推荐指数
1
解决办法
1511
查看次数

从android通过蓝牙浏览Raspberry Pi的文件系统

使用案例:

我正在开发一个涉及Android手机和Raspberry Pi的项目.Raspberry Pi单元将在现场种植传感器,偶尔用户可以随身携带电话并尝试从Raspberry Pi获取读数.所以应用程序的职责是获取数据并绘制图表.

设计:

我打算允许用户在设备配对后选择文件的位置.截至目前,已经完成了可用设备的识别,设备的程序化配对.但我无法从Android浏览Raspberry Pi的文件系统.你是如何实现这一目标的?

任何帮助,将不胜感激.如果您指向此资源,请同时解释(摘自此处的选定答案).

android bluetooth file-transfer android-bluetooth raspberry-pi2

11
推荐指数
1
解决办法
1396
查看次数

如何从RecyclerView.Adapter中的ViewHolder调用MainActivity方法?

GitHub的一个简单的应用程序项目中,我只有2个自定义Java文件:

  1. MainActivity.java包含与蓝牙和UI相关的源代码
  2. DeviceListAdapter.java包含AdapterViewHolder用于显示蓝牙设备RecyclerView

应用截图

当用户点击蓝牙设备时,MainActivity.java包含一个要调用的方法RecyclerView:

public void confirmConnection(String address) {
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to pair to " + device + "?");
    builder.setPositiveButton(R.string.button_ok, 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            device.createBond();
        }
    });
    builder.setNegativeButton(R.string.button_cancel, null);
    builder.show();
}
Run Code Online (Sandbox Code Playgroud)

ViewHolder类(在DeviceListAdapter.java中)中定义了单击侦听器:

public class DeviceListAdapter extends
  RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {

  private ArrayList<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();

  protected static …
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth android-viewholder android-recyclerview

11
推荐指数
2
解决办法
1万
查看次数

Android中的BLE数据包格式

根据BLE专利,BLE包中的数据大小为47字节.但是,Android只公开20个字节的数据.

android bluetooth-lowenergy android-bluetooth

11
推荐指数
1
解决办法
3476
查看次数

Android停止查找BLE设备:onClientRegistered() - status = 133 clientIf = 0

我正在开发一个应用程序,我可以在其中查找和配置BLE设备.我使用标准的Android BLE API,但最近我遇到了一些奇怪的问题.

当我打开我的应用程序时,BLE扫描工作正常.我正在扫描使用:

mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below
Run Code Online (Sandbox Code Playgroud)

mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above
Run Code Online (Sandbox Code Playgroud)

在Logcat中我收到以下消息(我猜这对于这个问题很重要):

D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,我还可以从我的BLE设备中读取某些特征(例如电池状态).我使用以下命令连接到设备以在单独的片段中读取此特征:

mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
Run Code Online (Sandbox Code Playgroud)

正确读取特征.在onCharacteristicRead回调中我也断开并关闭Gatt:

mBluetoothGatt.disconnect();
mBluetoothGatt.close();
Run Code Online (Sandbox Code Playgroud)

每次打开一个片段来读取一个特征(无论它是否是同一个设备),clientIf值都会增加.我可以在LogCat中看到:

D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
Run Code Online (Sandbox Code Playgroud)

一切正常,直到clientIf值等于10. BLE扫描停止查找任何设备,我无法连接到我的任何设备读取任何特征等.而LogCat无限显示这些消息:

D/BluetoothGatt: unregisterApp() - mClientIf=0 …
Run Code Online (Sandbox Code Playgroud)

android android-wireless android-bluetooth android-ble

11
推荐指数
1
解决办法
7703
查看次数

如何从蓝牙LE设备获取数据

我有一个支持蓝牙LE的蓝牙条码扫描器,我试图在扫描时从中获取条形码信息.

我可以连接到它很好onServicesDiscovered在我的调用,BluetoothGattCallback但我不知道该怎么做.

通过经典的蓝牙连接,您可以InputStream从a 获得BluetoothSocket,您只需等待read()给您提供数据,但我不确定它如何与蓝牙LE配合使用.我试图通过循环中BluetoothGattCharacteristic的检查属性,如果它的读取性能我打电话gatt.readCharacteristic(characteristic);,但只是给我无用的信息之前,我尝试扫描的东西,甚至.

那么如何从扫描仪获取条形码数据?

这是我的扫描仪https://www.zebra.com/us/en/support-downloads/scanners/ultra-rugged-scanners/li3608-li3678.html

android bluetooth-lowenergy android-bluetooth

11
推荐指数
1
解决办法
2541
查看次数