Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BLE和HC-05(带C代码)数据发送和接收?_Android_Bluetooth Lowenergy_Hc 05 - Fatal编程技术网

Android BLE和HC-05(带C代码)数据发送和接收?

Android BLE和HC-05(带C代码)数据发送和接收?,android,bluetooth-lowenergy,hc-05,Android,Bluetooth Lowenergy,Hc 05,在我的项目中,我使用Android和HC-05蓝牙。我需要向HC-05模块发送一条消息,然后向我发回一些数据 所以我创建了一个类似这样的项目: public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.

在我的项目中,我使用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]));
                }
            }
        };}
问题是我只收到一个号码。。。您可以在变量=11上看到这一点

I/Bluetooth设备:连接GATT E/设备名称::ETNA 1-1234 电子设备地址:6C:EC:EB:22:37:8D I/信息:已连接到TNA 1-1234 I/信息:发现 D/消息:写入完成 D/信息:特点[B@66005a2书面的 D/尺寸::1 D/变量: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或更多位。

根据HC-05,当您尝试使用蓝牙低能量(BLE)访问它时,它正在使用蓝牙2.0版(蓝牙经典),它是在Bluetooth 4.0版中引入的。您必须使用不同的模块或使用经典的Bluetooth对其进行写入。

HC-06模块也不是BLE模块。请看一看蓝牙查找和连接设备的经典方式:HM-10模块似乎是一个BLE mdoule,可能适合您。或者您可以切换microcont滚动到一个esp32,它也提供现成的BLE如果该设备仅为Bluetooth Classic,那么Android BLE代码连接到了什么设备?我从未尝试过通过BLE连接到classics设备,所以我不确定。我只能假设它连接到了正确的设备,只是写和读的工作方式不同你可以使用B像nRF Connect这样的LE扫描仪可以搜索您附近的BLE设备
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';
}