我已经使用接收器来监视这些意图一段时间了,以监视活动的蓝牙连接。我现在发现,在运行 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) 无论如何可以从支持配置文件(HDD、Spp 和音频)中获取连接的设备列表。要求就像我的设备将支持 HDD、SPP 和音频,所以我必须过滤支持所有这些配置文件的设备。反正有过滤设备吗?
我正在尝试从我的 android 应用程序连接到 Bleno Periphecal GATT 服务器。
GATT 服务器具有带有唯一 UUID 的自定义服务和特性。
如何确定连接到此服务器并发送一些文本?
最小SDK是21,目标SDK是24,所以旧的BluetoothLE Scanning方法被弃用,现在我需要使用BluetoothLEScanner。
android bluetooth bluetooth-lowenergy android-bluetooth gatt
我需要ble
在未连接到我的设备时永久扫描广告以找到它并知道何时连接到它(特定产品)。此扫描在前台服务中实现以匹配 8.0 先决条件。
为了节省一些电池,我想定期扫描(同时尊重 Android 7 的启动量/时间限制)。我看到 2 个不同的实现:
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) 我是 Flutter 新手,我正在开发一个需要蓝牙连接到我的硬件的项目。据我所知,Flutter_Blue 的工作原理如下:
scan()
,同时挂接一个侦听器来侦听新设备。(这将返回 ScanedDevice,然后将其添加到我的列表中。)await device.connect()
device.discoverServices()
for characteristics in service.characteristics
从该设备获取特征。characteristic.read().listen((value){//do something with value});
我在第二阶段(扫描设备)一直失败。我不知何故找不到我的蓝牙扬声器scan()
,我尝试使用不同的扫描模式(低延迟、低能耗、平衡)。
我可以轻松找到不属于我的其他设备(但它们不知何故没有名称,所有 ID 都是唯一的)。我尝试使用我朋友的手机作为我的应用程序进行扫描的设备,但仍然没有结果。
通过我的 Android 蓝牙设置页面,我可以在扫描新设备页面中找到蓝牙扬声器(也是我朋友的手机)。
请告诉我出了什么问题,我将非常感激。谢谢。
环境:
编译方式:flutter run
查看回复后学到的教训:
android bluetooth bluetooth-lowenergy android-bluetooth flutter
开发一个 Android 应用程序,通过 BLE 将数据发送到外围设备。关于 Android BluetoothGATTCallback onCharacteristicWrite 函数的问题 - 它如何知道写入事务已成功?只要不发生错误就可以认为成功吗?或者它是否记录从写入数据的外围设备特征发送的某种成功响应?
在扫描 BLE 设备期间,BluetoothAdapter 的方法 startLeScan 有时会返回 false。因此,我在我的应用程序中遇到了问题。我从 Lollipop 开始使用较新的方法 startScan(ScanCallback callback),但需要支持 API 级别 18 及以上。感谢任何帮助。
android bluetooth-lowenergy android-bluetooth ibeacon-android
我更改了代码
BluetoothAdapter.startLeScan
Run Code Online (Sandbox Code Playgroud)
至
BluetoothLeScanner.startScan(callback)
Run Code Online (Sandbox Code Playgroud)
因此,我认为扫描性能已足够提高,但我不知道bluetoothLeScanner的扫描时间。
BluetoothAdapter.startLeScan
扫描时间不断变化。
请回答。
我想询问有关对具有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
并且可以给我一些提示?谢谢
我正在使用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) 我目前正在从蓝牙传感器读取数据,因此数据会实时变化并且不断变化。我已将数据存储在变量中: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 ×10
bluetooth ×2
rxandroidble ×2
flutter ×1
gatt ×1
java ×1
kotlin ×1
mocking ×1
unit-testing ×1