在我的项目中,我使用 Android 和 HC-05 蓝牙。我需要向 HC-05 模块发送一条消息,然后将一些数据发回给我。
所以我创建了一个这样的项目:
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if(bluetoothAdapter == null)
{
Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
finish();
return;
}
scanLeDevice();
}
private BluetoothLeScanner bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
private boolean mScanning;
private Handler handler = new Handler();
UUID u1 = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");
private static final long SCAN_PERIOD = 2000;
private void scanLeDevice()
{
if(!mScanning)
{
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
mScanning = false;
bluetoothLeScanner.stopScan(leScanCallBack);
}
}, SCAN_PERIOD);
mScanning = true;
bluetoothLeScanner.startScan(leScanCallBack);
}
else
{
mScanning = false;
bluetoothLeScanner.stopScan(leScanCallBack);
}
}
private String Device_name = "";
private String Device_address;
private ScanCallback leScanCallBack = new ScanCallback()
{
@Override
public void onScanResult(int callbackType, ScanResult result)
{
super.onScanResult(callbackType, result);
if(result.getDevice().getName() != null && result.getDevice().getName().contains("ETNA") && result.getDevice().getName() != Device_name)
{
Device_name = String.valueOf(result.getDevice().getName());
Device_address = String.valueOf(result.getDevice().getAddress());
result.getDevice().connectGatt(MainActivity.this, false, bluetoothGattCallback);
Log.e("Device Name : ", Device_name);
Log.e("Device address", Device_address);
Button button = findViewById(R.id.button);
button.setText(Device_name);
//bluetoothLeScanner.stopScan(leScanCallBack);
//Log.e("Scan", "STOP");
}
}
};
BluetoothGattCallback bluetoothGattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i("Message", "Connected to" + Device_name);
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i("Message", "Disconnected from GATT server.");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
super.onServicesDiscovered(gatt, status);
Log.i("Message", "Discover");
if(status == BluetoothGatt.GATT_SUCCESS)
{
List<BluetoothGattService> services = gatt.getServices();
for(BluetoothGattService service : services)
{
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for(BluetoothGattCharacteristic characteristic : characteristics)
{
if(characteristic.getUuid().compareTo(u1) == 0)
{
characteristic.setValue("up2" + '\r' + '\n');
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
gatt.writeCharacteristic(characteristic);;
Log.d("Message", "Write done");
}
}
}
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
{
super.onCharacteristicWrite(gatt, characteristic, status);
Log.d("Message","Characteristic " + characteristic.getValue() + " written");
gatt.readCharacteristic(characteristic);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
byte[] value = characteristic.getValue();
for(int i = 0; i < value.length; i++)
{
Log.d("Size : ", String.valueOf(value.length));
Log.d("Variable : " + i, String.valueOf(value[i]));
}
}
};}
我将 ServicesDiscovered 函数上的字符串代码“up2”发送到我的 HC-05 模块
characteristic.setValue("up2" + '\r' + '\n');
问题是我只收到 1 个号码......你可以在变量 = 11 上看到
I/BluetoothDevice: connectGatt E/Device Name :: ETNA 1-1234 E/Device address: 6C:EC:EB:22:37:8D I/Message: Connected to ETNA 1-1234 I/Message: Discover D/Message: Write完成 D/消息:特征 [B@66005a2 写入 D/Size :: 1 D/Variable : 0:11
我的 HC-05 上的 C 代码是:
if (buffUART[0] == 'u' && buffUART[1] == 'p' && buffUART[2] == '2')
{
uint8_t string[30]={0, };
string[0] = (uint8_t)(nb_up->value >> 24);
string[1] = (uint8_t)(nb_up->value >> 16);
string[2] = (uint8_t)(nb_up->value >> 8);
string[3] = (uint8_t)(nb_up->value >> 0);
string[4] = (uint8_t)(nb_down->value >> 24);
string[5] = (uint8_t)(nb_down->value >> 16);
string[6] = (uint8_t)(nb_down->value >> 8);
string[7] = (uint8_t)(nb_down->value >> 0);
string[8] = (uint8_t)(nb_reset->value >> 24);
string[9] = (uint8_t)(nb_reset->value >> 16);
string[10] = (uint8_t)(nb_reset->value >> 8);
string[11] = (uint8_t)(nb_reset->value >> 0);
string[12] = (uint8_t)(nb_stop_lvl_0->value >> 24);
string[13] = (uint8_t)(nb_stop_lvl_0->value >> 16);
string[14] = (uint8_t)(nb_stop_lvl_0->value >> 8);
string[15] = (uint8_t)(nb_stop_lvl_0->value >> 0);
string[16] = (uint8_t)(nb_stop_lvl_1->value >> 24);
string[17] = (uint8_t)(nb_stop_lvl_1->value >> 16);
string[18] = (uint8_t)(nb_stop_lvl_1->value >> 8);
string[19] = (uint8_t)(nb_stop_lvl_1->value >> 0);
string[20] = (uint8_t)(nb_stop_lvl_2->value >> 24);
string[21] = (uint8_t)(nb_stop_lvl_2->value >> 16);
string[22] = (uint8_t)(nb_stop_lvl_2->value >> 8);
string[23] = (uint8_t)(nb_stop_lvl_2->value >> 0);
string[24] = (uint8_t)(nb_stop_lvl_3->value >> 24);
string[25] = (uint8_t)(nb_stop_lvl_3->value >> 16);
string[26] = (uint8_t)(nb_stop_lvl_3->value >> 8);
string[27] = (uint8_t)(nb_stop_lvl_3->value >> 0);
string[16] = '\r';
string[17] = '\n';
HAL_UART_Transmit(&husart3, string, 18, HAL_MAX_DELAY);
buffUART[0] = '0';
buffUART[1] = '0';
}
我不知道我错在哪里。以及我可以向手机发送多少数据,例如 8、16、32 位或更多位。