2016-07-12 13 views
0

BLEを使用してデータをストリーミングしようとしましたが、デバイスが接続されたままになりません。現在、サービスと特性のすべてのUUIDを接続して印刷し、接続を確立できます。非常に短時間BLEデバイスに接続した後、デバイスはアプリケーションに接続されません。BLE接続中止

import android.annotation.SuppressLint; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothGatt; 
import android.bluetooth.BluetoothGattCallback; 
import android.bluetooth.BluetoothGattCharacteristic; 
import android.bluetooth.BluetoothGattDescriptor; 
import android.bluetooth.BluetoothGattService; 
import android.bluetooth.BluetoothProfile; 
import android.bluetooth.BluetoothSocket; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Collection; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Queue; 
import java.util.Set; 
import java.util.UUID; 
import java.util.concurrent.Semaphore; 

public class ConnectActivity extends AppCompatActivity { 

    private BluetoothAdapter mBluetoothAdapter; 
    Set<BluetoothDevice> pairedDevices; 
    BluetoothDevice connectDevice; 
    BluetoothGatt bluetoothGatt; 
    List<BluetoothGattService> services; 
    private Queue<BluetoothGattCharacteristic> chars; 

    private static final String DAQSERVICE = "FFD7"; 

    private static final String SAMPLING_RATE_UUID = "FFD8"; 
    private static final String DATA_UUID = "FFD9"; 
    private static final String BATTERY_UUID = "FFDA"; 
    private static final String CONTROL_UUID = "FFDB"; 
    private static final String TAG = "error"; 

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

     Button btn = (Button) findViewById(R.id.connect_button); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     chars = new LinkedList<BluetoothGattCharacteristic>(); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        streamData(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 
    } 

    //pop up bluetooth settings menu if we cannot find device 
    void streamData() throws IOException { 
     for (BluetoothDevice device : pairedDevices) { 
      if (device.getName().equals("Connection Test")) { 

       connectDevice = device; 
       mBluetoothAdapter.cancelDiscovery(); 
       System.out.println("connecting to gatt"); 
       bluetoothGatt = connectDevice.connectGatt(this, false, btleGattCallback); 
      } 
     } 
    } 

    @SuppressLint("New API") 
    private BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { 
     @Override 
     public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 
      //this will get called anytime you perform a read or write characteristic operation 
      byte[] data = characteristic.getValue(); 
      System.out.print("data buffer updated: "); 
      for (int i = 0; i < data.length; i++) { 
       System.out.print(data[i]); 
      } 
      System.out.println(); 
     } 

     @Override 
     public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
      // this will get called when a device connects or disconnects 
      if (newState == BluetoothProfile.STATE_CONNECTED) { 
       discoverServices(); 
      } 
     } 

     @Override 
     public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { 
      // this will get called after the client initiates a BluetoothGatt.discoverServices() call 
      //getServices(); 
      Thread workerThread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        getServices(); 
       } 
      }); 
      workerThread.start(); 
      try{ 
       workerThread.join(); 
       System.out.println("joined thread"); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

     public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 
      byte[] data = descriptor.getValue(); 
      System.out.print("data buffer updated: "); 
      for (int i = 0; i < data.length; i++) { 
       System.out.print(data[i]); 
      } 
      System.out.println(); 
     } 

     public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      System.out.println("in read"); 
      if (status == BluetoothGatt.GATT_SUCCESS) { 
       System.out.println("UUID: " + characteristic.getUuid()); 
       byte[] data = characteristic.getValue(); 
       System.out.println("reading"); 
       for (byte temp : data) { 
        System.out.print(temp); 
       } 
      } 
     } 

     public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){ 
      System.out.println("in write"); 
      if (status == BluetoothGatt.GATT_SUCCESS) { 
       System.out.println("UUID: " + characteristic.getUuid()); 
       byte[] data = characteristic.getValue(); 
       System.out.println("reading in write mode"); 
       System.out.println(new String(data)); 
      } 
     } 
    }; 

    void discoverServices() { 
     System.out.println("discovering services"); 
     bluetoothGatt.discoverServices(); 
    } 

    void getServices() { 
     System.out.println("getting services"); 
     services = bluetoothGatt.getServices(); 
     System.out.println("number of services: " + services.size()); 
     for (BluetoothGattService service : services) { 
      List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); 
      System.out.println("service uuid: " + service.getUuid().toString()); 
      for (BluetoothGattCharacteristic characteristic : characteristics){ 
        System.out.println("characteristic uuid: " + characteristic.getUuid().toString()); 

        bluetoothGatt.setCharacteristicNotification(characteristic, true); 
        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) { 
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
        } 
        bluetoothGatt.readCharacteristic(characteristic); 
      } 
     } 
     System.out.println("got all services"); 
    } 
} 

答えて

0

問題がどこにあるかを知る必要があります。

リモートデバイスですか?他の携帯電話やアプリはあなたのリモートデバイスに接続できますか? RFとリモートデバイスのクロックもチェックできます。

関連する問題