标签: android-bluetooth

对蓝牙使用 ACL_CONNECTED 和 ACL_DISCONNECTED 似乎不适用于 Android 4.2.2。有什么解决办法吗?

我已经使用接收器来监视这些意图一段时间了,以监视活动的蓝牙连接。我现在发现,在运行 android 4.2.2 的 Galaxy S4 上,这些意图似乎根本不存在。

它适用于我的 Transformer Infinity(4.1)、Droid Bionic(4.1) 和 Droid X(2.3.3)。4.2.2 中是否有某些更改破坏了此方法?

这是我的代码供参考。

public class BluetoothReceiver extends BroadcastReceiver
{
    public static boolean BluetoothConnected;

    public void onReceive(Context context, Intent intent)
    {

        Log.d(TAG, "Bluetooth Intent Recieved");

        String action = intent.getAction();
        Log.d(TAG, "Bluetooth Called: Action: " + action);

        if (action.equalsIgnoreCase("android.bluetooth.device.action.ACL_CONNECTED"))
        {
            Log.d(TAG, "BLUETOOTH CONNECTED RECIEVED");
            BluetoothConnected = true;
        }

        if (action.equalsIgnoreCase("android.bluetooth.device.action.ACL_DISCONNECTED"))
        {
            Log.d(TAG, "BLUETOOTH DISCONNECTED RECIEVED");
            BluetoothConnected = false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth

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

如何从特定的蓝牙配置文件中获取连接的设备列表

无论如何可以从支持配置文件(HDD、Spp 和音频)中获取连接的设备列表。要求就像我的设备将支持 HDD、SPP 和音频,所以我必须过滤支持所有这些配置文件的设备。反正有过滤设备吗?

android android-bluetooth bluetooth-profile

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

Android 应用程序:使用 UUID 连接到 GATT 服务器

我正在尝试从我的 android 应用程序连接到 Bleno Periphecal GATT 服务器。

GATT 服务器具有带有唯一 UUID 的自定义服务和特性。

如何确定连接到此服务器并发送一些文本?

最小SDK是21,目标SDK是24,所以旧的BluetoothLE Scanning方法被弃用,现在我需要使用BluetoothLEScanner。

android bluetooth bluetooth-lowenergy android-bluetooth gatt

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

RxAndroidBle:定期广告扫描

我需要ble在未连接到我的设备时永久扫描广告以找到它并知道何时连接到它(特定产品)。此扫描在前台服务中实现以匹配 8.0 先决条件。

为了节省一些电池,我想定期扫描(同时尊重 Android 7 的启动量/时间限制)。我看到 2 个不同的实现:

  • 每 X 秒开始扫描,Y 秒后可以停止,等待时间为 XY。

    ?? ?? ?? ?? ?? 扫描???? ?? ?? ?? ?? 等待???? ?? ?? ?? ??扫描???? ?? ?? ?? ?? 等待
    |--------------|------|--------------|------|---
    | ---------X-------->|---------X-------->|---
    |------你----->?? ?? ?? ?? ?? ?? ?? ?? |-----Y----->?? ?? ?? ?? ?? ?? ?? ?? |---


  • 开始扫描 X 秒,等待 Y 秒,开始扫描 X 秒等等。

    ?? ?? ?? ?? ?? 扫描???? ?? ?? ?? ?? 等待???? ?? ?? ?? ??扫描???? ?? ?? …

android bluetooth-lowenergy android-bluetooth rxandroidble

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

BluetoothAdapter.getDefaultAdapter().startDiscovery() 不起作用

public void startScan() {
        final List<MyBluetoothDevice> arrayOfFoundBTDevices = new ArrayList<>();

        // start looking for bluetooth devices

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean scanStatus = mBluetoothAdapter.startDiscovery();


        Timber.d("SCANNING_STATUS : " + scanStatus);

        // Discover new devices
        // Create a BroadcastReceiver for ACTION_FOUND
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Timber.d("onReceiveSignal");
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the bluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                } else if …
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth

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

Flutter_Blue 无法扫描我的设备。(我的BT音箱)Android

我是 Flutter 新手,我正在开发一个需要蓝牙连接到我的硬件的项目。据我所知,Flutter_Blue 的工作原理如下:

  1. 获取 Flutter_Blue 实例
  2. 使用 开始扫描scan(),同时挂接一个侦听器来侦听新设备。(这将返回 ScanedDevice,然后将其添加到我的列表中。)
  3. 从我的列表中选择一个我想要连接的设备await device.connect()
  4. 连接成功后,发现其服务device.discoverServices()
  5. for characteristics in service.characteristics从该设备获取特征。
  6. 使用characteristic.read().listen((value){//do something with value});

我在第二阶段(扫描设备)一直失败。我不知何故找不到我的蓝牙扬声器scan(),我尝试使用不同的扫描模式(低延迟、低能耗、平衡)。

我可以轻松找到不属于我的其他设备(但它们不知何故没有名称,所有 ID 都是唯一的)。我尝试使用我朋友的手机作为我的应用程序进行扫描的设备,但仍然没有结果。

通过我的 Android 蓝牙设置页面,我可以在扫描新设备页面中找到蓝牙扬声器(也是我朋友的手机)。

请告诉我出了什么问题,我将非常感激。谢谢。

环境:

  • 索尼 XZ2(安卓版本:9)
  • 颤振版本:2.0.5
  • 飞镖版本:2.12.3
  • SDK:“>=2.7.0 <3.0.0”
  • 扑蓝:0.6.3+1
  • IDE:VSCode

编译方式:flutter run

查看回复后学到的教训:

  1. Bluetooth_Low_Energy(BLE) 无法发现/连接到经典蓝牙。反之亦然(经典 -> BLE)。

android bluetooth bluetooth-lowenergy android-bluetooth flutter

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

如果没有发生错误,是否认为 Android BluetoothGattCallback onCharacteristicWrite 函数成功?

开发一个 Android 应用程序,通过 BLE 将数据发送到外围设备。关于 Android BluetoothGATTCallback onCharacteristicWrite 函数的问题 - 它如何知道写入事务已成功?只要不发生错误就可以认为成功吗?或者它是否记录从写入数据的外围设备特征发送的某种成功响应?

bluetooth-lowenergy android-bluetooth

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

startLeScan 返回 false

在扫描 BLE 设备期间,BluetoothAdapter 的方法 startLeScan 有时会返回 false。因此,我在我的应用程序中遇到了问题。我从 Lollipop 开始使用较新的方法 startScan(ScanCallback callback),但需要支持 API 级别 18 及以上。感谢任何帮助。

android bluetooth-lowenergy android-bluetooth ibeacon-android

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

blueLeothLeScanner扫描时间多长时间?

我更改了代码

BluetoothAdapter.startLeScan
Run Code Online (Sandbox Code Playgroud)

 BluetoothLeScanner.startScan(callback)
Run Code Online (Sandbox Code Playgroud)

因此,我认为扫描性能已足够提高,但我不知道bluetoothLeScanner的扫描时间。

BluetoothAdapter.startLeScan 扫描时间不断变化。

请回答。

android android-bluetooth

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

Android单元测试:模拟BluetoothDevice并写入parcel

我想询问有关对具有BluetoothDevice变量的类进行单元测试的问题。

我的对象是一个简单的对象,其中包含一些原始变量和一个 BluetoothDevice 变量。我的对象也是可包裹的。

最终我的代码在移动设备上运行得很好,但是当我运行单元测试时出现了奇怪的错误。

我在测试类中模拟了BluetoothDevice,如下所示:

@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class BluetoothDeviceTest {

    @Mock
    BluetoothDevice device1;

    @Before
    public void initMocks(){
        MockitoAnnotations.initMocks(this);

        when(device1.getAddress()).thenReturn("01:02:03:04:05:06");
        when(device1.getName()).thenReturn("device767b1");
        when(device1.getBondState()).thenReturn(BOND_NONE);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的对象以及其他原语中,我使用:

  • 我用的是out.writeParcelable(mBluetoothDevice,0);内部@Override public void writeToParcel(Parcel out, int flags)方法。

  • 我使用mBluetoothDevice = in.readParcelable(BluetoothDevice.class.getClassLoader());内部protected MyObject(Parcel in)构造函数。

测试我的对象实例的分割的单元测试失败并出现异常 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

请再次注意,当我的代码运行完全正常并且打包在我使用的移动应用程序中运行良好时。只有单元测试表现得很奇怪。

我怀疑这是因为我的模拟BluetoothDevice变量在包裹中比正常的真实实例短,并且包裹中数据字段的顺序变得混乱。

有没有人用mocked进行过单元测试BluetoothDevice并且可以给我一些提示?谢谢

java android unit-testing mocking android-bluetooth

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

使用库RxAndroidBle从Android设备读取多个特征

我正在使用RxAndroidBle库来扫描设备,然后连接到一个特定的设备并读取4 GATT特征。

我可以通过以下代码读取一个特性(电池电量):

scanSubscription = rxBleClient.scanBleDevices(
            new ScanSettings.Builder()
                    .build()
    )
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(
                    scanResult -> {
                        if(scanResult.getBleDevice().getName() != null){
                            if(scanResult.getBleDevice().getName().equals("NODE 1")){
                                Log.e("BLE SCAN", "SUCCESS");
                                Log.e("BLE SCAN", scanResult.getBleDevice().getName());
                                Log.e("BLE SCAN", scanResult.getBleDevice().getMacAddress());
                                scanSubscription.unsubscribe();

                                RxBleDevice device = scanResult.getBleDevice();
                                subscription = device.establishConnection(false) // <-- autoConnect flag
                                        .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb")))
                                        .subscribe(
                                                characteristicValue -> {
                                                    Log.e("Characteristic", characteristicValue[0]+"");
                                                },
                                                throwable -> {
                                                    Log.e("Error", throwable.getMessage());
                                                }
                                        );
                            }
                        }
                    }
            )
            .subscribe();
Run Code Online (Sandbox Code Playgroud)

我可以使用来阅读两个:

.flatMap(rxBleConnection -> Observable.combineLatest( // use the same connection and combine latest emissions
                rxBleConnection.readCharacteristic(aUUID),
                rxBleConnection.readCharacteristic(bUUID),
                Pair::new
        )) …
Run Code Online (Sandbox Code Playgroud)

bluetooth-lowenergy android-bluetooth rxandroidble

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

如何将实时变化的数据从activity传递到fragment?

我目前正在从蓝牙传感器读取数据,因此数据会实时变化并且不断变化。我已将数据存储在变量中:liveData:ByteArray

现在我尝试将 liveData 从 MainActivity 发送到 Sensordisplayfragment。

更新

根据@CTD的评论,这就是我尝试过的,不幸的是我对viewModel没有太多了解,而且在线研究很混乱,因为似乎有很多方法来实现viewModel。

在我存储变量 liveData 的 MainActivity 类中:

val model:MyViewModel by viewModels()
    private fun processLiveData(liveData : ByteArray){
        livedata = liveData
        model.uploadData(livedata)
    }
Run Code Online (Sandbox Code Playgroud)

在 MyViewModel.class 中,viewModel 位于:

class MyViewModel: ViewModel() {
    private val realtimedata = MutableLiveData<ByteArray>()

    fun uploadData(data:ByteArray){
        realtimedata.value = data
    }

    fun loadData():LiveData<ByteArray>{
        return realtimedata
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,在我获取数据的 Sensordisplay 片段中:

val 模型:MyViewModel by viewModels()

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    model.loadData().observe(viewLifecycleOwner,Observer<ByteArray>{
        passandprocessLiveData(it)
    })
    return inflater.inflate(R.layout.sensordisplay, container, false)
} …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-bluetooth android-runonuithread

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