java – Android BLE外围设备数据接收

这可能听起来很基本,但我是
Android BLE开发的初学者.

到目前为止,我能够将Nexus 9设备创建为外围设备,将Moto G设置为Central.另外,我正在成功连接设备.但我无法弄清楚,当我从中央设备发送一个特性时,它将从外设接收到什么?广告回叫仅在广告成功启动时才返回(在我的情况下成功)

这是我的外围代码

        btleGattCallback = new BluetoothGattCallback() {

         @Override
            // Result of a characteristic read operation
            public void onCharacteristicRead(BluetoothGatt gatt,
                    BluetoothGattCharacteristic characteristic,
                    int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                }
            }        
        @Override
        public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
            // this will get called when a device connects or disconnects
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w("doscover", "onServicesDiscovered received: " + status);
            }
        }
        };

BeaconParser bp = new BeaconParser().setBeaconLayout("m:2-3=0,i:4-19,p:20-20");

        mBeaconTransmitter = new BeaconTransmitter(this, bp );

        // Transmit a beacon with Identifiers 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 2
        Long li = new Long(-0l);
        ArrayList<Long> l = new ArrayList<Long>();
        l.add(li);
        Beacon beacon = new Beacon.Builder()

                  .setId1("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
                  .setId2("1")
                  .setId3("2")
                  .setManufacturer(0x00ff) // Choose a number of 0x00ff or less as some devices cannot detect beacons with a manufacturer code > 0x00ff
                  .setTxPower(-59)
                  .setDataFields(l)
                  .build();

        mBeaconTransmitter.startAdvertising(beacon);

和中央法典相似

    btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE) ;
    madapter = btManager.getAdapter();
    if (madapter != null && !madapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);   
        startActivityForResult(enableIntent,1);
    }
    mHandler = new Handler();

    btleGattCallback = new BluetoothGattCallback() {

         @Override
            // Result of a characteristic read operation
            public void onCharacteristicRead(BluetoothGatt gatt,
                    BluetoothGattCharacteristic characteristic,
                    int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                }
            }        
        @Override
        public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
            Log.w("doscover", "Connected " + status);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.w("discover", "onServicesDiscovered received: " + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w("doscover", "onServicesDiscovered received: " + status);
            }
        }






    };


    devicefound = new ArrayList<BluetoothDevice>();
    devices = new ArrayAdapter<String>( this , R.layout.device_name);
    ListView pairedListView = (ListView) findViewById(R.id.textView3);
    pairedListView.setAdapter(devices);

     mleScanCallback = new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
                    devicefound.add(device);

                        //UUID = device.getAddress().toString();
                        //Log.e("Search", "" + device.getName().toString() );
                        //Toast`.makeText(context,device.getName().toString(), 1 ).show();
                        Log.e("Search", "" + device.toString());
                        Log.e("Search", "" + String.valueOf(rssi)  );


                            devices.add("Nex" + device.getName() );

                            //mBluetoothGatt = device.connectGatt(getActivity(), true, btleGattCallback);        




            }
        };

        pairedListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View arg1,
                    int position, long id) {


                 dv = devicefound.get(position);

                 mBluetoothGatt = dv.connectGatt(getApplicationContext() , true, btleGattCallback);        
                 mBluetoothGatt.connect();
                 dv.createBond();

                 //BluetoothDevice dd = madapter.getRemoteDevice(devicefound.get(position).getAddress());
                if(i == false)
                {
                    Toast.makeText(getApplicationContext(), "Service found. Connected to " + dv.getName() , 1).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Already Connected " + dv.getName() , 1).show();

                }
                 i = mBluetoothGatt.connect();

                mBluetoothGatt.beginReliableWrite();

                boolean b = mBluetoothGatt.discoverServices();
                Log.e("Discovery Started", "" + b );
                     Log.e("get ItemATPosition", "" + adapter.getItemAtPosition(position));

                     //BluetoothGattService Service = mBluetoothGatt.getService(  );
                     // = Service.getCharacteristic(k);

                     BluetoothGattCharacteristic characteristic= new BluetoothGattCharacteristic(k,2,1); //Service
                     characteristic.setValue( "This is a Charects ");



                     byte[] value = {(byte)91,(byte)92,(byte)93};

                     characteristic.setValue(value); 

                     boolean st = mBluetoothGatt.writeCharacteristic(characteristic); 
                     //Toast.makeText(getActivity(), t1.getText() + " " + st, 1).show();
                     boolean b1 = mBluetoothGatt.executeReliableWrite();


                     //Toast.makeText(getActivity(), t1.getText() + " " + b1, 1).show();
                     Intent in = new Intent("ACTION_BOND_STATE_CHANGED.");
                    sendBroadcast(in);

            }
        });
        scanLeDevice(true);

我正在使用来自中央的连接Gatt的writeCharacteristic命令但是不知道如何从外围端接收

任何形式的帮助将不胜感激.

最佳答案 外围模式可能对IOS有好处,我建议您使用浅蓝色进行测试,从这里:
https://itunes.apple.com/us/app/lightblue-bluetooth-low-energy/id557428110?mt=8

据我所知,在nexus 6和nexus 9上安装的Lollipop 5可以支持外设模式,nexus 5不支持它.

Moto G支持外设模式?

点赞