2017-07-08 1 views
1

J'ai le code de travail suivant qui est capable de numériser et se connecter à un périphérique BLE:Comment communiquer avec BLE

import android.Manifest; 
import android.app.AlertDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothGatt; 
import android.bluetooth.BluetoothGattCallback; 
import android.bluetooth.BluetoothGattCharacteristic; 
import android.bluetooth.BluetoothGattService; 
import android.bluetooth.BluetoothManager; 
import android.bluetooth.le.BluetoothLeScanner; 
import android.bluetooth.le.ScanCallback; 
import android.bluetooth.le.ScanResult; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class MainActivity2 extends AppCompatActivity { 

    private final static String TAG = MainActivity2.class.getSimpleName(); 

    BluetoothManager btManager; 
    BluetoothAdapter btAdapter; 
    BluetoothLeScanner btScanner; 
    private final static int REQUEST_ENABLE_BT = 1; 
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; 

    Boolean btScanning = false; 
    int deviceIndex = 0; 
    ArrayList<BluetoothDevice> devicesDiscovered = new ArrayList<BluetoothDevice>(); 
    BluetoothGatt bluetoothGatt; 

    public final static String ACTION_GATT_CONNECTED = 
      "com.example.bluetooth.le.ACTION_GATT_CONNECTED"; 
    public final static String ACTION_GATT_DISCONNECTED = 
      "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; 
    public final static String ACTION_GATT_SERVICES_DISCOVERED = 
      "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"; 
    public final static String ACTION_DATA_AVAILABLE = 
      "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; 
    public final static String EXTRA_DATA = 
      "com.example.bluetooth.le.EXTRA_DATA"; 

    public Map<String, String> uuids = new HashMap<String, String>(); 

    // Stops scanning after 5 seconds. 
    private Handler mHandler = new Handler(); 
    private static final long SCAN_PERIOD = 5000; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
     btAdapter = btManager.getAdapter(); 
     btScanner = btAdapter.getBluetoothLeScanner(); 

     if (btAdapter != null && !btAdapter.isEnabled()) { 
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
     } 

     // Make sure we have access coarse location enabled, if not, prompt the user to enable it 
     if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("This app needs location access"); 
      builder.setMessage("Please grant location access so this app can detect peripherals."); 
      builder.setPositiveButton(android.R.string.ok, null); 
      builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
       @Override 
       public void onDismiss(DialogInterface dialog) { 
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); 
       } 
      }); 
      builder.show(); 
     } 

     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    // Device scan callback. 
    private ScanCallback leScanCallback = new ScanCallback() { 
     @Override 
     public void onScanResult(int callbackType, ScanResult result) { 
      devicesDiscovered.add(result.getDevice()); 
      deviceIndex++; 
     } 
    }; 

    // Device connect call back 
    private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { 

     @Override 
     public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 

     } 

     @Override 
     public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 

     } 

     @Override 
     public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { 
      displayGattServices(bluetoothGatt.getServices()); 
     } 

     @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 onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      // >> DUNNO WHAT TO DO HERE << 
     } 
    }; 

    private void broadcastUpdate(final String action, 
           final BluetoothGattCharacteristic characteristic) { 

     System.out.println(characteristic.getUuid()); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case PERMISSION_REQUEST_COARSE_LOCATION: { 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        System.out.println("coarse location permission granted"); 
       } else { 
        final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setTitle("Functionality limited"); 
        builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background."); 
        builder.setPositiveButton(android.R.string.ok, null); 
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 

         @Override 
         public void onDismiss(DialogInterface dialog) { 
         } 

        }); 
        builder.show(); 
       } 
       return; 
      } 
     } 
    } 

    public void startScanning() { 
     System.out.println("start scanning"); 
     btScanning = true; 
     deviceIndex = 0; 
     devicesDiscovered.clear(); 
     AsyncTask.execute(new Runnable() { 
      @Override 
      public void run() { 
       btScanner.startScan(leScanCallback); 
      } 
     }); 

     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       stopScanning(); 
      } 
     }, SCAN_PERIOD); 
    } 

    public void stopScanning() { 
     System.out.println("stopping scanning"); 
     btScanning = false; 
     AsyncTask.execute(new Runnable() { 
      @Override 
      public void run() { 
       btScanner.stopScan(leScanCallback); 
      } 
     }); 
    } 

    public void connectToDeviceSelected() { 
     int deviceSelected = Integer.parseInt("0" /* MY DEVICE INDEX TAKEN FROM AN EDITTEXT*/); 
     bluetoothGatt = devicesDiscovered.get(deviceSelected).connectGatt(this, false, btleGattCallback); 
    } 

    public void disconnectDeviceSelected() { 
     bluetoothGatt.disconnect(); 
    } 

    private void displayGattServices(List<BluetoothGattService> gattServices) { 
     if (gattServices == null) return; 

     // Loops through available GATT Services. 
     for (BluetoothGattService gattService : gattServices) { 
      new ArrayList<HashMap<String, String>>(); 
      List<BluetoothGattCharacteristic> gattCharacteristics = 
        gattService.getCharacteristics(); 

      // Loops through available Characteristics. 
      for (BluetoothGattCharacteristic gattCharacteristic : 
        gattCharacteristics) { 
       // SHOWS AVAILABLES 
      } 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.joelwasserman.androidbleconnectexample/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.joelwasserman.androidbleconnectexample/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 

Maintenant, une fois connecté, je veux envoyer des données (en octets format []) à l'appareil BLE ... Comment puis-je atteindre cet objectif? Merci à l'avance

Obs .: Je API ciblant> = 21

Répondre

1

Découvrez un exemple d'écrire une nouvelle valeur dans caractéristique:

public static final byte NO_ALERT = 0x00; //your byte data 
public final static UUID IMMEDIATE_ALERT_UUID = 
     UUID.fromString("00001802-0000-1000-8000-00805f9b34fb"); //UUID of gatt service 
public final static UUID IMMEDIATE_ALERT_LEVEL_UUID = 
     UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb"); //UUID of characteristic 

@Override 
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 

    byte[] data; 
    byte b = NO_ALERT; 
    data = new byte[]{b}; 
    BluetoothGattService service = gatt.getService(IMMEDIATE_ALERT_UUID); 
    if (service != null) { 
     BluetoothGattCharacteristic characteristic = service.getCharacteristic(IMMEDIATE_ALERT_LEVEL_UUID); 
     characteristic.setValue(data); 
     gatt.writeCharacteristic(characteristic); 
    } 
}