BLE(蓝牙低能耗)

时间:2019-07-24 12:02:33

标签: android

    I try to implement Connection between two devices(Any ios/Android)    and transfer text using BLE

    I try too many demos from GitHub and also try from documentation step by step but I am not getting a success

    some time give Error like :

    > GATT server 133

    or

    >android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)'    on a null object reference

    Please help me

    Thank you so much

    Here is the my two activity code one ble class and one activity  to call function from BLE claass

    I want just pass small text to android to android or android to ios using ble

    please help I tried from too many days

================================================================
public class MainActivity extends AppCompatActivity {
    ArrayList<String> getDevicess;
    List<BluetoothDevice> getConnectedDevicess;
    TextView tvscan;
    TextView tvConnected;
    TextView tvsend;
    Ble ble;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvsend = (TextView) findViewById(R.id.tvsend);
        getDevicess = new ArrayList<>();
        getConnectedDevicess = new ArrayList<>();
        ble = new Ble(this, getApplicationContext(), "zad");
        ble.enableBle();
        ble.checkPermission(0);
        ble.scanLeDevice(true, 1000);

   //after scan and connect click on send button then give error
        tvsend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ble.writeCharacteristics(hexStringToByteArray("abcdefg"), UUID.fromString("fffffff0-00f7-4000-b000-000000000000"), UUID.fromString("fffffff5-00f7-4000-b000-000000000000"));

            }
        });
    }

    public byte[] hexStringToByteArray(String s) {
        int len = s.length()-1;
        byte[] data = new byte[len / 2];

        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }

        return data;
    }
}


===========================================BLE CLass==================

//写入特征的功能..!     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)     公共无效writeCharacteristics(byte []数据,UUID DLE_SERVICE,UUID DLE_WRITE_CHAR){         BluetoothGattService服务= mBluetoothGatt.getService(DLE_SERVICE);         如果(服务==空){             Log.d(TAG,“找不到服务!”);             //返回false;         }

    BluetoothGattCharacteristic charc1 = Service.getCharacteristic(DLE_WRITE_CHAR);
    if (charc1 == null) {
        Log.d(TAG, "char not found!");
        Log.d(TAG, "CHARAC_-TRST" + charc1.getUuid());
        // return false;
    }
    // charc1.setValue(new byte[]{0x00, 0x05, 0x10, 0x01, 0x3E, 0x01, 0x23});

    charc1.setValue(data);
    boolean stat = mBluetoothGatt.writeCharacteristic(charc1);

    Log.d(TAG, "FINISHED WRITTING CHAR 1 status write :(status)" + stat);

    if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for (byte byteChar : data)
            stringBuilder.append(String.format("%02X ", byteChar));


    }


}

1 个答案:

答案 0 :(得分:0)

此代码段有问题:

   public void displayBleService(List<BluetoothGattService> gattServices) {
       Log.d(TAG, "display Services");
       List<BluetoothGattCharacteristic> characteristics = new ArrayList<BluetoothGattCharacteristic>();
       for (BluetoothGattService services : gattServices) {
           Log.d(TAG, "SERVICES == ." + services.getUuid());

           characteristics = services.getCharacteristics();
           for (BluetoothGattCharacteristic characteristic : characteristics) {
               Log.d(TAG, "CAHRACTERISTICS  == ." + characteristic.getUuid() + "--" + characteristic.getProperties());


               //enableNotification(characteristic, true);
           }
       }

   }

services为空,因此services.getCharacteristics()激怒了NPE。

请调试您的代码,并确保您也具有BLUETOOTH_ADMIN权限。也可以自行决定添加必要的null-guards。