2017-01-23 112 views
0

我試圖將BLE設備(Adafruit羽毛M0 Bluefruit LE)連接到Android應用程序。羽毛有一個傳感器發送特殊傳感器事件的UART文本。將BLE設備連接到Android發送數據(nUART)

我想

  • 連接到該設備(靜態MAC地址)
  • 連接到北歐-UART服務
  • 從羽毛
  • 實現連接的監控接收文本

現在我想了解BLE是如何工作的。我理解它只要連接到設備,但在那之後我不明白。

  1. 如何連接到服務或自動連接到服務?
    1. 我需要什麼來獲取傳入數據的數據或通知?

這是現在的代碼。現在有檢查,所以當事情沒有按計劃發生時,它會崩潰。但我試圖瞭解BLE的事情。另外它不是一項服務,只是一項活動。我無法測試它,因爲智能手機不到一週的時間。

package de.example.bluetoothletest; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothGatt; 
import android.bluetooth.BluetoothGattCallback; 
import android.bluetooth.BluetoothGattCharacteristic; 
import android.bluetooth.BluetoothManager; 
import android.bluetooth.BluetoothProfile; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.content.LocalBroadcastManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import java.util.UUID; 

public class MainActivity extends AppCompatActivity { 

private BluetoothAdapter mBluetoothAdapter; 
private BluetoothGatt mBluetoothGatt; 
private String deviceaddress=("FC:57:1A:B7:22:AD"); 
public final static String ACTION_DATA_AVAILABLE = "de.example.BluetoothLETest.ACTION_DATA_AVAILABLE"; 
public static final UUID RX_SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); 
public static final UUID RX_CHAR_UUID = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"); 
public static final UUID TX_CHAR_UUID = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); 
public static String EXTRA_DATA="de.example.BluetoothLETest.EXTRA_DATA"; 


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



    //get the BluetoothAdapter 
    final BluetoothManager mbluetoothManager=(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
    mBluetoothAdapter=mbluetoothManager.getAdapter(); 

} 
public void connect(){ 
    //connect to the given deviceaddress 
    BluetoothDevice device=mBluetoothAdapter.getRemoteDevice(deviceaddress); 
    mBluetoothGatt=device.connectGatt(this, false, mGattCallback); 
    //now we get callbacks on mGattCallback 
} 

//get callbacks when something changes 
private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     if (newState== BluetoothProfile.STATE_CONNECTED){ 
      //device connected 
     } 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     if (status==BluetoothGatt.GATT_SUCCESS){ 
      //all Services have been discovered 
     } 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     //we are still connected to the service 
     if (status==BluetoothGatt.GATT_SUCCESS){ 
      //send the characteristic to broadcastupdate 
      broadcastupdate(ACTION_DATA_AVAILABLE, characteristic); 
     } 
    } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
     //send the characteristic to broadcastupdate 
     broadcastupdate(ACTION_DATA_AVAILABLE, characteristic); 
    } 
}; 

//Get the 'real' data out of characteristic 
private void broadcastupdate(final String action,final BluetoothGattCharacteristic characteristic){ 
    final Intent intent= new Intent(action); 
    //only when it is the right characteristic?/service? 
    if (TX_CHAR_UUID.equals(characteristic.getUuid())){ 
     //get the 'real' data from the stream 
     intent.putExtra(EXTRA_DATA, characteristic.getValue()); 
     //send the extracted data via LocalBroadcastManager 
     LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 

    } 

} 
} 

此代碼是否適用於智能手機接收的內容?如何連接到特定的服務,或者由於設備只提供一項服務,我該如何自動完成?

+0

請等到您的智能手機到達,然後詢問您的具體問題。 –

回答

0

你的連接方式改成這樣:

if (newState == BluetoothProfile.STATE_CONNECTED) { 
        intentAction = ACTION_GATT_CONNECTED; 
        broadcastUpdate(intentAction); 
        gatt.discoverServices(); 
       } 

和serviceDiscoverd

@Override 
     public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
      super.onServicesDiscovered(gatt, status); 
      List<BluetoothGattService> services = gatt.getServices(); 
      gatt.readCharacteristic(services.get(1).getCharacteristics().get(0)); 
      String uuid; 
      List<BluetoothGattCharacteristic> gattCharacteristics; 
      ArrayList<BluetoothGattCharacteristic> charas; 
      for (BluetoothGattService gattService : services) { 
       gattCharacteristics = 
         gattService.getCharacteristics(); 
       charas = new ArrayList<>(); 
       for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { 
        charas.add(gattCharacteristic); 
        uuid = gattCharacteristic.getUuid().toString(); 
        if (uuid.equals("0000fff4-0000-1000-8000-00805f9b34fb")) { 
         final int charaProp = gattCharacteristic.getProperties(); 
         if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { 
          BluetoothGattCharacteristic mNotifyCharacteristic = gattCharacteristic; 
          setCharacteristicNotification(
            gattCharacteristic, true); 
         } 
        } 
       } 
      } 
     } 

public void setCharacteristicNotification(final BluetoothGattCharacteristic characteristic, 
               boolean enabled) { 
     if (mBluetoothAdapter == null || mGatt == null) { 
      return; 
     } 
     mGatt.setCharacteristicNotification(characteristic, enabled); 

     Log.e("uuid service", characteristic.getUuid() + ""); 
     String uuid = "0000fff2-0000-1000-8000-00805f9b34fb"; 

     if (uuid.equals(characteristic.getUuid().toString())) { 
      Log.e("uuid service2", characteristic.getUuid() + ""); 
      BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
        UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); 
      if (descriptor != null) { 
       descriptor.setValue(enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00}); 
       mGatt.writeDescriptor(descriptor); 
      } 
     } 
    } 

現在終於卡讀取特點:

@Override 
     public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      super.onCharacteristicRead(gatt, characteristic, status); 
      updateStatus(characteristic); 
      Log.e("gatt", "readChar"); 
     } 

     @Override 
     public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      super.onCharacteristicWrite(gatt, characteristic, status); 
      updateStatus(characteristic); 
      Log.e("gatt", "writeChar"); 
     } 

     @Override 
     public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
      super.onCharacteristicChanged(gatt, characteristic); 
      updateStatus(characteristic); 
      Log.e("gatt", "changeChar"); 
     } 
+0

(charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY)> 0總是返回true。你可能是指&。 – Emil

+0

沒有讓你@Emil –

相關問題