2017-08-03 12 views
0

私は現在、Bluetooth低エネルギーを使用してIOSデバイスに接続してデータを相互に転送できるAndroid用アプリを開発中です。アンドロイドデバイスは私の中心となり、IOSデバイスはペリフェラルになります。しかし、このアプリはしばらく前に作成されたため、デバイスを検索するstartLeScanメソッドは廃止されました。いくつかの調査をした後、私はstartScan()を使うべき新しいメソッドであると理解しています。しかし、私は自分のプログラムでこの新しいメソッドを実装するのに苦労しました。誰かが新しいメソッドを組み込むように自分のコードを編集できますか?Bluetooth low energy startLeScan廃止されました

MainActivity.java

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothGatt; 
import android.bluetooth.BluetoothGattCallback; 
import android.bluetooth.BluetoothGattCharacteristic; 
import android.bluetooth.BluetoothGattDescriptor; 
import android.bluetooth.BluetoothGattService; 
import android.bluetooth.BluetoothManager; 
import android.bluetooth.BluetoothProfile; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 
import java.util.List; 


public class MainActivity extends AppCompatActivity { 

    private BluetoothAdapter bluetoothAdapter; 
    private BluetoothGatt gatt; 
    private BluetoothGattCharacteristic inputCharacteristic; 
    private TextView outputView; 
    private EditText inputView; 
    private static final int REQUEST_ENABLE_BT = 1; 
    BluetoothAdapter.LeScanCallback leScanCallback; 



*** public void receiveMode(View v) { 
     bluetoothAdapter.startLeScan(leScanCallback); 
    } *** 

    public void sendMessage(View v) { 
     inputCharacteristic.setValue(inputView.getText().toString()); 
     gatt.writeCharacteristic(inputCharacteristic); 
    } 


    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { 
     @Override 
     public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 

      if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) { 
       final String value = characteristic.getStringValue(0); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         outputView.setText(value); 
        } 
       }); 

      } 
     } 
     @Override 
     public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
      MainActivity.this.gatt = gatt; 
      if (newState == BluetoothProfile.STATE_CONNECTED) { 
       try { 
        Thread.sleep(1500); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       gatt.discoverServices(); 
      } 

     } 
     @Override 
     public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
      List<BluetoothGattService> services = gatt.getServices(); 
      for (BluetoothGattService service : services) { 
       List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); 
       for (BluetoothGattCharacteristic characteristic : characteristics) { 
        if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) { 
         try { 
          Thread.sleep(500); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         gatt.setCharacteristicNotification(characteristic, true); 
         BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0); 
         if (descriptor != null) { 
          descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
          gatt.writeDescriptor(descriptor); 
         } 
        } else if (getString(R.string.inputUUID).equals(characteristic.getUuid().toString())) { 
         try { 
          Thread.sleep(500); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         inputCharacteristic = characteristic; 
        } 
       } 
      } 


     } 
    }; 



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

     outputView = (TextView) findViewById(R.id.outputText); 
     inputView = (EditText) findViewById(R.id.inputText); 

     final BluetoothManager bluetoothManager = 
       (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
     bluetoothAdapter = bluetoothManager.getAdapter(); 

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

    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="uk.ac.york.androidtoios2.MainActivity"> 

    <EditText 
     android:id="@+id/inputText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:onClick="sendMessage" 
     android:text="Send Message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:onClick="receiveMode" 
     android:text="Connect" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/outputText" 
     android:text="Message" 
     android:gravity="center" 
     android:textSize="24sp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

参照:[タグを誤って使用するのを避けるにはどうすればいいですか](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) – EJoshuaS

答えて

1

交換

 bluetoothAdapter.startLeScan(leScanCallback); 

bluetoothAdapter.getBluetoothLeScanner().startScan(new ScanCallback() { 
@Override 
public void onScanResult(int callbackType, ScanResult result) { 
super.onScanResult(callbackType, result); 
Log.i(TAG, "Remote device name: " + result.getDevice().getName()); 
    } 
}); 
+0

私はこの新しいIOSデバイスがペリフェラルに置かれているときはデバイスを見つけることができません。なぜこれが –

+0

であるのかわかりません。アンドロイドモニターは、120フレームがスキップされていると言います。アプリケーションがメインスレッドであまりにも多くの作業をしている可能性があります。問題は、自分のコードのどの部分が別のスレッドで実行されるのか不明です。 –

+0

IOSデバイスは積極的にその存在を宣伝していますか? ScanCallbackの他のメソッドをオーバーライドして、スキャンを開始する際にエラーが発生していないかどうかを確認します。 –

関連する問題