标签: android-ble

如何建立使用服务进行连接的BLE连接,以跨活动使用而不停止服务或断开ble?

我有3个组成部分。

  1. Activity1 具有用于连接和断开BLE连接的按钮

  2. Activity2 需要从BLE设备获取数据。

  3. 服务 所有连接逻辑(例如getRemoteDevice(),connectGatt等)都属于服务。

Activity1通过绑定服务连接到BLE设备。

Intent gattServiceIntent = new Intent(mContext,BleService.class);//In Activity1 context
bindService(gattServiceIntent, mServiceConnection,BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

并在按下按钮后立即连接到ble设备。

现在,当我从Activity1移到Activity2时,我将取消绑定Activity1中的服务。

mContext.unbindService(mServiceConnection);//In Activity1 context
Run Code Online (Sandbox Code Playgroud)

现在如何在Activity2中使用现有的BLE设备连接?

我的临时解决方案:

当新的服务实例从Activity2上下文绑定到Activity2时,我将重新连接 BLE设备。(我不想要。)

Activity2中,我正在检查我的服务是否已经在运行(如果未运行),那么我将从Activity2上下文中再次绑定该服务。

if(!isMyServiceRunning(BleWrapper.class)){
    Intent wrapperServiceIntent = new Intent(mContext,BleWrapper.class);    
    bindService(wrapperServiceIntent,mBLEWrapperServiceConnection,BIND_AUTO_CREATE);
    }else{
        Log.w(LOGTAG, "Service already connected. In onCreate");
    }
Run Code Online (Sandbox Code Playgroud)

在ServiceConnection回调下的onServiceConnected()中触发连接

@Override
public void onServiceConnected(ComponentName componentName,IBinder service)     {

    mBluetoothLeService = ((BleWrapper.LocalBinder) service).getService();

    if (!mBluetoothLeService.initialize()) {
        showAlertDialog(getString(R.string.ble_not_supported_on_this_device));
    }else {
        mBluetoothLeService = …
Run Code Online (Sandbox Code Playgroud)

android android-service bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
2150
查看次数

当getBluetoothLeAdvertiser返回一个对象时,为什么isMultipleAdvertisementSupported()返回false?

我正试图在我的设备上玩BLE传输.

这是我使用的代码和输出:

// check BLE support
Log.i(TAG, "BLE supported: " + getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)); // true

// check BLE transmission support
final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

Log.i(TAG, "isMultipleAdvertisementSupported: " + mBluetoothAdapter.isMultipleAdvertisementSupported()); // false
Log.i(TAG, "isOffloadedFilteringSupported: " + mBluetoothAdapter.isOffloadedFilteringSupported()); // false
Log.i(TAG, "isOffloadedScanBatchingSupported: " + mBluetoothAdapter.isOffloadedScanBatchingSupported()); // false

BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
Log.i(TAG, mBluetoothLeAdvertiser.toString()); //android.bluetooth.le.BluetoothLeAdvertiser@1c51f789

// check BLE transmission support
// android-beacon-library, https://github.com/AltBeacon/android-beacon-library
int result = BeaconTransmitter.checkTransmissionSupported(getApplicationContext());
Log.i(TAG, "ABL checkTransmissionSupported: " + result); // 0
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么mBluetoothLeAdvertiser不是 …

android sony ibeacon-android android-ibeacon android-ble

4
推荐指数
1
解决办法
3653
查看次数

什么是Android M +可以拥有的最大并发Ble连接数

我的应用程序需要同时连接9个Ble设备.在本文和任何其他资源中,它写的android 4.4+只能连接到7个设备.M或N版本有什么新东西吗?谢谢.

bluetooth-lowenergy android-ble

4
推荐指数
2
解决办法
3049
查看次数

BLE 堆栈的开源实现

除了 BlueZ 和 Bluedroid 之外,还有其他 BLE 堆栈的开源实现吗?

bluetooth bluetooth-lowenergy bluez android-ble

4
推荐指数
1
解决办法
8245
查看次数

Android BLE Gatt连接更改状态

我有一个Android应用程序连接到BLE设备并写入它.我可以成功连接,读取和写入它.作为测试的一部分,我们正在尝试不同的断开连接.

有时,如果ble设备断开连接,我将连接更改为断开连接,状态值为19.此外,如果存在任何绑定错误,则状态等于22.如果我以编程方式断开连接,则此状态为0.但没有除了0之外的这些状态在android文档中指定.

发布示例BluetoothGattCallback

private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        Log.i(TAG, "onConnectionStateChange status: "+status+", newState: "+newState);
        /*i need to know the posiible values for this status variable*/
        if(newState == BluetoothProfile.STATE_CONNECTED) {
            gatt.discoverServices();
        } else {
            gatt.close();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.i(TAG, "onServicesDiscovered service discovered");
    }
};
Run Code Online (Sandbox Code Playgroud)

有没有人遇到同样的问题,并整理出状态列表.我需要知道onConnectionStateChange方法中状态变量的可能值

android bluetooth android-bluetooth android-ble bluetooth-gatt

3
推荐指数
1
解决办法
8327
查看次数

客户端特征配置描述符中的ENABLE_INDICATION_VALUE和ENABLE_NOTIFICATION_VALUE是什么?

我一直在开发一个应用程序,以便从另一个BLE设备读取和写入数据。从应用程序的角度来看,我没有任何问题。我只需要澄清一些概念上的疑问。所以:

  1. 为什么我们需要cccd来启用通知?

  2. 一旦通过ENABLE_INDICATION_VALUE,它是否在外围设备中启用通知?

  3. ENABLE_INDICATION_VALUE和ENABLE_NOTIFICATION_VALUE之间的主要区别是什么,而两者都执行相同的任务,即向中央设备发送连续数据?

android bluetooth-lowenergy android-bluetooth android-ble

3
推荐指数
1
解决办法
1475
查看次数

连接到 AND UA651BLE 时获取状态为 133 的 Type_Gatt_Error

我正在尝试连接到 AnD UA-651BLE 血压监测器并在 android 应用程序中获取值。该应用程序能够找到该设备,但我在“onConnectionStateChange”中收到 Type_Gatt_Error。

这适用于某些设备,如三星 Galaxy S5、Moto G4 等,但不适用于某些手机,如红米手机 3s prime 等。

这很好用,我能够读取数据,当我取消设备配对并每次重新配对时。我什么都不懂,因为我是 android-ble 集成的新手。我尝试将目标 SDK 版本更改为 21 和 23,因为我主要使用 android 5.0.1 及更高版本的设备。但没有运气

我发现门户网站中有许多人提出了类似的问题,但没有找到任何适合我的解决方案。

任何人都可以让我理解为什么会发生这种情况以及可能的解决方案是什么?

以下是我在 Android Studio 中得到的日志:

D/MYTAG:  DashBoard UnBonded Device false true
D/MYTAG:  Found device - =*******************A&D_UA-651BLE_459701
D/SN: RESU connectDevice device 6C:EC:EB:45:97:01
D/BluetoothManager: getConnectionState()
D/BluetoothManager: getConnectedDevices
W/SN: RESU Attempt to connect in state: 0
D/BluetoothGatt: connect() - device: 6C:EC:EB:45:97:01, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=e7ed0993-f27a-4e2a-bc08-93b76d55a4d5
D/SN: RESU bluetoothGatt android.bluetooth.BluetoothGatt@47015f2
D/MYTAG:  CAlling connectDevice …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-studio android-ble bluetooth-gatt

3
推荐指数
1
解决办法
2235
查看次数

BLE 通告数据大小限制

在我的 Addroid 应用程序上,当我尝试启动 ble 广告时,我尝试添加一些额外的数据,正如我所读到的,广告数据必须 <= 31 字节。

我就是这样做的:

var testData = "abcdefghij"
var data = AdvertiseData.Builder().apply {
               addServiceData(
                   ParcelUuid(MY_UUID),
                   testData.toByteArray(Charsets.UTF_8)
               )
           }
bleAdvertiser.startAdvertising(
    settings.build(),
    data.build(),
    advertiseCallback
)
Run Code Online (Sandbox Code Playgroud)

这样一切都运转良好。现在如果testData字段变成

var testData = "abcdefghijk"
Run Code Online (Sandbox Code Playgroud)

由于超出广告数据大小限制,广告开始失败。

如果单个字符占用 2 个字节,为什么我有 11 个字符的字符串却超出了 30 个字节的限制?

kotlin bluetooth-lowenergy android-bluetooth android-ble

3
推荐指数
1
解决办法
2437
查看次数

Android:添加ScanCallback让我"不幸的是,App已经停止了." 找不到MainActivity

我是android dev的新手.我看过一些youtube视频,并阅读了一些文档.今天就像第5天,所以要耐心等待我.我正在尝试构建一个BluetoothLE应用程序.我从这里偷了一些代码,但是当我尝试在我的设备上运行时.我得到一个"不幸的是,(应用程序名称)已停止"消息.在评论出不同的代码块之后,我将其归结为以下声明:

private ScanCallback mScanCallback = new ScanCallback() {};
Run Code Online (Sandbox Code Playgroud)

如果我包含该行,它就会崩溃.如果我不这样做,它就会存在.这是完整的Java类:

package com.tremor.creech.bluetoothExample;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.List;
@TargetApi(21)
public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private int REQUEST_ENABLE_BT = 1;
    private Handler mHandler;
    private static final long SCAN_PERIOD = 10000;
    private BluetoothLeScanner mLEScanner;
    private ScanSettings settings;
    private List<ScanFilter> filters;
    private BluetoothGatt mGatt;

    @Override
    protected void …
Run Code Online (Sandbox Code Playgroud)

android bluetooth android-ble

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

如何正确使用UUID.fromString方法?

我正在尝试读/写这些特征:

在此输入图像描述

现在,我正在尝试阅读AA01*

我正在使用这个库来做到这一点.

这是我的代码:

private void connectToSensorTag(RxBleDevice rxBleDevice) {
    rxBleDevice.establishConnection(getApplicationContext(), false)
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    int i = 0;
                }
            })
            .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString("AA01*")))
            .subscribe(new Subscriber<byte[]>() {
                @Override
                public void onCompleted() {
                    int i = 0;
                }

                @Override
                public void onError(Throwable e) {
                    int i = 0;
                }

                @Override
                public void onNext(byte[] bytes) {
                    int i = 0;
                }
            });
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

java.lang.IllegalArgumentException:无效的UUID:AA01*

我也尝试过这个类似问题的解决方案,但没有任何效果.同样的错误.

java android bluetooth-lowenergy android-ble rxandroidble

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

哪个targetSDK用于BLE?

我正在构建一个Android应用程序,它应该在每个具有蓝牙低功耗的设备上运行,这意味着minSDK为18.我不确定我应该使用哪个targetSDK.我在网上看到,最好始终使用最新版本(API 22).是这种情况,还是我应该使用我支持的每个targetSDK构建我的应用程序,即使用SDK 18为运行API 18的应用程序构建,使用SDK 19为具有API 19的设备构建,......?

我很困惑因为开始BLE发现我可以使用startScan()或startLeScan().Android文档告诉我:"startLeScan()在API级别21中已弃用,请使用startScan()".我不确定这对我应该使用哪个targetSDK来编译我的应用程序有什么影响.如果我使用SDK 22编译并使用startScan(),运行API 18的设备是否能运行我的应用程序,如果我使用API​​ 18编译并使用startLeScan(),运行API 22的设备是否能够运行我的应用程序?或者我应该真的只使用我支持的每个targetSDK构建我的应用程序,如上所述?

android bluetooth-lowenergy target-sdk android-ble

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

通过 BLE 将 Android 应用程序连接到 Raspberry Pi 3 时遇到问题

我使用 Raspberry Pi 3 B 型作为蓝牙外设。Pi 正在运行 GATT 服务器并对其进行广告。

我可以从 LightBlue iOS 应用程序连接到它,并且可以读取和写入值。

我在通过 BLE 从 Android 应用程序连接到 Pi 时遇到问题。我尝试使用 Play 商店中的 2 个应用程序 - BLE 扫描仪和蓝牙 LE 扫描仪。BluetoothLeGatt 是我尝试使用的另一个应用程序。它可作为 Android Studio 上的示例 BLE 项目使用。pi 出现在扫描结果中,一旦我按下连接,应用程序就会尝试连接,但可能会被 pi 拒绝连接。程序控制转到 GattCallback 函数,状态更改为“断开连接”,并且服务发现永远不会发生。日志如下所示:

D/BluetoothGatt: connect() - device: B8:27:EB:A4:E7:75, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=0c46767a-1ddc-4d91-83fe-490f9d3a5ad7
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothLeService: Trying to create a new connection.
D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=B8:27:EB:A4:E7:75
I/BluetoothLeService: Disconnected from GATT server.
Run Code Online (Sandbox Code Playgroud)

同一应用程序能够连接到使用 LightBlue iOS 应用程序或 …

android bluetooth bluetooth-lowenergy android-ble raspberry-pi3

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

Android BLE 扫描永远找不到设备

几天以来,我尝试在我的应用程序中实现 BLE 连接。我知道我尝试连接的设备功能齐全,因此问题一定是我的代码。

我用的是BluetoothLeScanner.startScan()方法。
但回调方法永远不会被调用。

   public void startScan() {
        if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
            isScanning = true;
            Handler mHandler = new Handler();
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mainActivityHandler.setMSG("Scan stopped");
                    isScanning = false;
                    leScanner.stopScan(scanCallback);
                }
            }, SCAN_TIME);

            mainActivityHandler.setMSG("Start scan");

            try {

                leScanner.startScan(scanCallback);
            } catch (Exception e) {
                mainActivityHandler.catchError(e);
            }

        } else mainActivityHandler.catchError(new Exception("Bluetooth not activated"));
    }
Run Code Online (Sandbox Code Playgroud)

我的 CallbackMethod (不知道我是否正确使用 gatt,但这是另一个问题):

    private ScanCallback scanCallback = new ScanCallback() {


    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        mainActivityHandler.setMSG("Callback"); …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-ble

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