现在我知道这个问题已经被问了十亿次了。
我已经通读了其中的大部分(这个网站对我的问题没有太大帮助),我已经通读了文档,我已经通读了示例和教程。
我想做的是:
将hello world发送到可移动设备(连接到arduino)
到目前为止我所做的:
与使用内置设置的设备配对(我的应用程序中没有编程逻辑)
使用GATT服务器与设备连接。
然而,当我试图编写一个特征时,相应的状态返回布尔值总是false。这是最相关的代码:
private void setupServer(BluetoothAdapter bAdapter) {
Log.d(TAG, "Started setting up server");
BluetoothLeAdvertiser bAdvertiser = bAdapter.getBluetoothLeAdvertiser();
if (bAdapter != null) {
bGattServer = bManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
}
});
//Create a service using a random UUID
//Create characteristics with WRITE property and write a value
BluetoothGattService service = new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic bluetoothGattCharacteristic = new BluetoothGattCharacteristic(bUUID, BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_WRITE);
Log.d(TAG, "Adding characteristic to service " + service.addCharacteristic(bluetoothGattCharacteristic));
bluetoothGattCharacteristic.setValue("hello");
Log.d(TAG, "Trying to write characteristic..., first value: " + bluetoothGattCharacteristic.getValue()[0]);
if (bGatt != null) {
boolean success = bGatt.writeCharacteristic(bluetoothGattCharacteristic);
if (success) {
Log.d(TAG, "Successfuly wrote characteristic to" + bUUID);
} else {
//THIS ALWAYS EXECUTES??
Log.e(TAG, "Failed to write to characteristic");
}
} else {
Log.e(TAG, "BGATT IS NULL");
}
service.addCharacteristic(bluetoothGattCharacteristic);
bGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
bGattServer.addService(service);
}
以下是我的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
这是其余的相关代码
private boolean scanBluetooth(final BluetoothAdapter bAdapter, final BluetoothDevice[] bDevice) {
final BluetoothLeScanner bScanner = bAdapter.getBluetoothLeScanner();
final boolean[] stopCallback = {false};
Log.d(TAG,"Started scanning");
bScanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
bDevice[0] = result.getDevice();
if (bDevice[0] == null || stopCallback[0]) {
Toast.makeText(getApplicationContext(), "Didn't find a device", Toast.LENGTH_LONG);
} else {
stopCallback[0] = true;
System.out.println("DEVICE" + bDevice[0]);
bGatt = bDevice[0].connectGatt(getApplicationContext(), true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS){
Log.d(TAG, "Successful GATT " + newState);
setupServer(bAdapter);
return;
}
if(status == BluetoothGatt.GATT_FAILURE){
Log.e(TAG,"Bigg error boi");
System.exit(1);
}
else{
Log.w(TAG,"Oh no, gatt failed " + status + " retrying");
return;
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "Characteristic written" + status);
}
}, BluetoothDevice.TRANSPORT_LE);
Log.d(TAG, "Bgatt value:" + bGatt);
}
}
});
if(bGatt==null){
Log.w(TAG, "BGATT IS NULL WHILE SCANNING");
}
Log.d(TAG, "Finished scanning...");
return true;
}
我使用应用程序BLEScanner获得了bUUID,它显示了一个用于读写的UUID。我的应用程序的目标是镜像该应用程序的功能,但我开始失去理智。我甚至不能发送一个简单的问候世界。
Android Studio输出:
018-12-01 23:58:56.214 25024-25136/com.iopt.davidv7.iopt W/DBG: Oh no, gatt failed 22 retrying
2018-12-01 23:58:56.675 25024-25136/com.iopt.davidv7.iopt D/DBG: Successful GATT 2
2018-12-01 23:58:56.675 25024-25136/com.iopt.davidv7.iopt D/DBG: Started setting up server
2018-12-01 23:58:56.692 25024-25136/com.iopt.davidv7.iopt D/DBG: Adding characteristic to service true
2018-12-01 23:58:56.693 25024-25136/com.iopt.davidv7.iopt D/DBG: Trying to write characteristic..., first value: 104
2018-12-01 23:58:56.693 25024-25136/com.iopt.davidv7.iopt E/DBG: Failed to write to characteristic
客户端是一个简单的HM-10蓝牙模块,我现在想使用ARDUINO IDE在串行监视器上镜像我在应用程序中发送的内容。
我希望我已经给了你所有的背景,因为我正在失去希望。我没有手动检查权限,因为我已经使用系统对话框设置了权限,但是,只有“位置”可以启用?这可能是我问题的根源吗?使用checkSelfPermission检查bluetooth,bluetooth admin返回true,所以我不认为是这样:
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_ADMIN)
!= PackageManager.PERMISSION_GRANTED) {
Log.e(TAG,"No permission for bluetooth");
// Permission is not granted
}
else{
Log.d(TAG,"Bluetooth permission granted");
}
似乎我的问题在于创建一个新服务,而不是使用发现服务,然后解析发现的服务。从算法上来说,你要做的是:1。触发器discoverServices(),这反过来会在回调2中触发OnServicesDiscovery()。然后,从发现的服务中创建一个服务,并从该服务中创建一个特性。因为我目前的硬件只支持一个特性,所以我只能提供第一个(也是唯一一个)服务。3.使用setValue 4为服务设置值。为服务增添特色
我不会深入解释我的readData代码是做什么的,但它可能会帮助某人弄清楚如何触发通知并从中读取数据。
private boolean scanBluetooth(final BluetoothAdapter bAdapter, final BluetoothDevice[] bDevice) {
final BluetoothLeScanner bScanner = bAdapter.getBluetoothLeScanner();
final boolean[] stopCallback = {false};
Log.d(TAG,"Started scanning");
bScanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
bDevice[0] = result.getDevice();
if (bDevice[0] == null || stopCallback[0]) {
Toast.makeText(getApplicationContext(), "Didn't find a device", Toast.LENGTH_LONG);
} else {
stopCallback[0] = true;
System.out.println("DEVICE" + bDevice[0]);
bGatt = bDevice[0].connectGatt(getApplicationContext(), true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS){
Log.d(TAG, "Successful GATT " + newState);
//Simply triggers a discoverService method, which in return triggers onServiceDiscovered, which writes sample data into the characteristic
setupClient(bAdapter);
return;
}
if(status == BluetoothGatt.GATT_FAILURE){
Log.e(TAG,"Bigg error boi");
System.exit(1);
}
else{
Log.w(TAG,"Oh no, gatt failed " + status + " retrying");
return;
}
}
@Override
//Triggered after we write data in a characteristic
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "Characteristic written" + status);
}
//Triggered when someone(the remote device if everything is set up correctly) changes a characteristic!
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.w(TAG,"Characteristic changed");
try {
Log.d(TAG,"Change characteristic" + new String(characteristic.getValue(),"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG,"Size of discovered" + gatt.getServices().size());
getData(gatt);
writeCharacteristic(gatt);
Log.d(TAG,"Services discovered");
}
}, BluetoothDevice.TRANSPORT_LE);
Log.d(TAG, "Bgatt value:" + bGatt);
}
}
});
if(bGatt==null){
Log.w(TAG, "BGATT IS NULL WHILE SCANNING");
}
Log.d(TAG, "Finished scanning...");
return true;
}
//Doesn't do much, starts discovering services, which triggers onServicesDiscovered
private void setupClient(BluetoothAdapter bAdapter) {
Log.d(TAG, "Started setting up server");
if (bAdapter != null) {
Log.d(TAG, "SERVICE DISCOVERED" + bGatt.discoverServices());
}
}
//Write characteristic into the discovered service
//There is only one characteristic, so we can just take the first index
private void writeCharacteristic(BluetoothGatt gatt){
BluetoothGattService bService = gatt.getService(bUUID);
BluetoothGattCharacteristic bChars = bService.getCharacteristics().get(0);
if(bChars != null){
bChars.setValue("Hello");
bService.addCharacteristic(bChars);
boolean success = gatt.writeCharacteristic(bChars);
if(success){
Log.d(TAG, "Write characteristic successful");
}
else{
Log.e(TAG, "Write characteristic failed");
}
}
else{
Log.e(TAG,"Characteristic not found");
}
}
//Set up notifications for the discovered service!
private void getData(BluetoothGatt gatt){
BluetoothGattService bService = gatt.getService(bUUID);
BluetoothGattCharacteristic bChars = bService.getCharacteristics().get(0);
gatt.setCharacteristicNotification(bChars,true);
}
将跟踪和跨度添加到Slf4J MDC,以便您可以从日志聚合器中的给定跟踪或跨度中提取所有日志。示例日志: 2016-02-02 15:30:57.902 INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ... 2016-02-02 15:30:58.372 ERROR [bar,6bfd
反缓存 anticache设置该选项后,它将删除可能引起服务器响应的Header(if-none-match和if-modified-since)304 not modified。当您要确保完全捕获HTTP交换时,这很有用。当您要确保服务器以完整的数据响应时,也经常在客户端重播期间使用它。 客户端重播 客户端重播可以做到:您提供了一个以前保存的HTTP对话,而mitmproxy则一个接一个地重播了
特征值
特征缩放是用来统一资料中的自变项或特征范围的方法,在资料处理中,通常会被使用在资料前处理这个步骤。 1 动机 因为在原始的资料中,各变数的范围大不相同。对于某些机器学习的算法,若没有做过标准化,目标函数会无法适当的运作。举例来说,多数的分类器利用两点间的距离计算两点的差异, 若其中一个特征具有非常广的范围,那两点间的差异就会被该特征左右,因此,所有的特征都该被标准化,这样才能大略的使各特征
我当前的Cucumber文件如下所示: 所以现在我想再添加几个场景,可能是在同一个文件中进行API测试。所以我想为此创建一个新特性,而不是使用Feature:Test Online application页面。这样我就不需要为API测试创建单独的特性文件。
trait(特征)类似于其他语言中的interface或者protocol,指定一个实际类型必须满足的功能集合 与interface不同的地方在于,interface会隐藏具体实现类型,而trait不会。在rust中,隐藏实现类型可以由generic配合trait作出。 Rust中的trait: trait关键字 trait对象