ご覧いただきありがとうございます。
- したいことはラズパイからandroidにBLE通信でテキストメッセージを送信することです
前提条件は以下の通りです。
- androidはセントラル(java)
- ラズパイはペリフェラル(python)
- エラーは現在特に出ていない
- javaは初めて触ります
現在できていること
- android側でスキャンをし、ラズパイのMACアドレスを検知すると、notifyを確立するところまで
- ラズパイ側でアドバタイズする
javaのプログラムは以下の通りです。(importは長いため省略してます)
デバッグをしていると、138行目のserviceがずっとnullみたいで、永遠に140行目の分岐に
入れていません。また、ログは、139行目のeeeで最後です。
私の推測ですが、UUIDの設定が間違っているのかな..?と思っています。
(UUIDは適当に生成したものを使っています。)
試したこと
IOSのlight blueを使って、正しくアドバタイズできているか、UUIDは設定されているかを確認し、
アプリ上で確認できました。
端末情報
Androidは、google Pixel 6
ラズパイは4 Bです。
あと少しのところで詰まってしまい、時間をかけましたが、
解決できそうにないので、わかる方いれば、教えてほしいです。
java
1package com.example.myapplication_blescan; 2 3public class MainActivity extends Activity { 4 private final static String TAG = MainActivity.class.getSimpleName(); 5 private static final int REQUEST_PERMISSION_CODE = 2; 6 7 private BluetoothAdapter mBluetoothAdapter; 8 private BluetoothLeScanner mBluetoothLeScanner; 9 private BluetoothGatt mBluetoothGatt; 10 private boolean mScanning; 11 12 private static final UUID MY_SERVICE_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 13 private static final UUID MY_CHARACTERISTIC_UUID = UUID.fromString("00002201-0000-1000-8000-00805F9B34FB"); 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 19 // BluetoothManagerを取得し、アダプターを取得する。 20 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 21 mBluetoothAdapter = bluetoothManager.getAdapter(); 22 Log.d(TAG, "aaa"); 23 // BLEがサポートされているか確認する。 24 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 25 finish(); 26 } 27 28 mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); 29 requestPermissions(); 30 31 } 32 33 private void requestPermissions() { 34 List<String> permissionsNeeded = new ArrayList<>(); 35 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 36 permissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); 37 } 38 if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { 39 permissionsNeeded.add(Manifest.permission.BLUETOOTH_SCAN); 40 } 41 if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { 42 permissionsNeeded.add(Manifest.permission.BLUETOOTH_CONNECT); 43 } 44 45 if (!permissionsNeeded.isEmpty()) { 46 ActivityCompat.requestPermissions(this, permissionsNeeded.toArray(new String[0]), REQUEST_PERMISSION_CODE); 47 } else { 48 scanLeDevice(true); 49 } 50 } 51 52 53 private void scanLeDevice(final boolean enable) { 54 if (enable) { 55 56 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { 57 58 // TODO: Consider calling 59 // ActivityCompat#requestPermissions 60 // here to request the missing permissions, and then overriding 61 // public void onRequestPermissionsResult(int requestCode, String[] permissions, 62 // int[] grantResults) 63 // to handle the case where the user grants the permission. See the documentation 64 // for ActivityCompat#requestPermissions for more details. 65 return; 66 } 67 Log.d(TAG, "bbb"); 68 mBluetoothLeScanner.startScan(mLeScanCallback); 69 mScanning = true; 70 } else { 71 mBluetoothLeScanner.stopScan(mLeScanCallback); 72 mScanning = false; 73 } 74 } 75 76 // スキャンのコールバック。 77 private ScanCallback mLeScanCallback = new ScanCallback() { 78 @Override 79 public void onScanResult(int callbackType, ScanResult result) { 80 super.onScanResult(callbackType, result); 81 // スキャン結果から、デバイスを取得し、Gattサーバーへ接続する。 82 BluetoothDevice device = result.getDevice(); 83 84// Log.d(TAG, String.valueOf(device)); 85// Log.d(TAG, "aaa"); 86 if (String.valueOf(device).equals("DC:A6:32:DE:76:7B")) { // 指定されたMACアドレスと比較 87 if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { 88 // TODO: Consider calling 89 // ActivityCompat#requestPermissions 90 // here to request the missing permissions, and then overriding 91 // public void onRequestPermissionsResult(int requestCode, String[] permissions, 92 // int[] grantResults) 93 // to handle the case where the user grants the permission. See the documentation 94 // for ActivityCompat#requestPermissions for more details. 95 return; 96 } 97 Log.d(TAG, device.getName()); 98 Log.d(TAG, "find!!!"); 99 mBluetoothGatt = device.connectGatt(MainActivity.this, false, mGattCallback); 100 Log.d(TAG, "iii"); 101 } 102 }; 103 104 105 // Gattコールバック。 106 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 107 @Override 108 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 109 Log.d(TAG, "lll"); 110 if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) { 111 Log.d(TAG, "ccc"); 112 // GATTサーバーに接続成功。サービスを探索する。 113 if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { 114 // TODO: Consider calling 115 // ActivityCompat#requestPermissions 116 117 // here to request the missing permissions, and then overriding 118 // public void onRequestPermissionsResult(int requestCode, String[] permissions, 119 // int[] grantResults) 120 // to handle the case where the user grants the permission. See the documentation 121 // for ActivityCompat#requestPermissions for more details. 122 return; 123 } 124 gatt.discoverServices(); 125 } else { 126 // GATTサーバーへの接続が切断または失敗。スキャンを再開する。 127 scanLeDevice(true); 128 } 129 } 130 131 @Override 132 public void onServicesDiscovered(BluetoothGatt gatt, int status) { 133 // 対象のサービスとキャラクタリスティックを見つけ、Notificationをセットする。 134 Log.d(TAG, String.valueOf(BluetoothGatt.GATT_SUCCESS)); 135 if (status == BluetoothGatt.GATT_SUCCESS) { 136 Log.d(TAG, "ddd"); 137 Log.d(TAG, String.valueOf(status)); 138 BluetoothGattService service = gatt.getService(MY_SERVICE_UUID); 139 Log.d(TAG, "eee"); //ここまで出力できている 140 if (service != null) { 141 BluetoothGattCharacteristic characteristic = service.getCharacteristic(MY_CHARACTERISTIC_UUID); 142 Log.d(TAG, "fff"); 143 if (characteristic != null) { 144 // Characteristicのプロパティを確認し、必要な処理を実装する 145 int properties = characteristic.getProperties(); 146 if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) != 0) { 147 if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { 148 // TODO: Consider calling 149 // ActivityCompat#requestPermissions 150 // here to request the missing permissions, and then overriding 151 // public void onRequestPermissionsResult(int requestCode, String[] permissions, 152 // int[] grantResults) 153 // to handle the case where the user grants the permission. See the documentation 154 // for ActivityCompat#requestPermissions for more details. 155 return; 156 } 157 gatt.readCharacteristic(characteristic); 158 } 159 if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { 160 gatt.setCharacteristicNotification(characteristic, true); 161 } 162 } 163 } 164 } 165 } 166 167 168 169 @Override 170 public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 171 Log.d(TAG, "ggg"); 172 if (status == BluetoothGatt.GATT_SUCCESS) { 173 // キャラクタリスティックが読まれた場合、データを取得する。 174 byte[] data = characteristic.getValue(); 175 if (data != null && data.length > 0) { 176 String textData = new String(data, Charset.forName("UTF-8")); 177 Log.i(TAG, "Received data: " + textData); 178 } 179 } 180 }; 181 }; 182} 183 184
pythonのプログラムを以下に示します。
制限の文字数を超えてしまうので、省略してます。
https://qiita.com/comachi/items/c494e0d6c6d1775a3748
上記の方のアドバタイズのプログラムを参考にさせていただきました。
ほとんど一緒だと思います。
python
1from pybleno import * 2 3bleno = Bleno() 4 5# サービスとキャラクタリスティックのためのUUID 6APPROACH_SERVICE_UUID = '00001101-0000-1000-8000-00805F9B34FB' 7APPROACH_CHARACTERISTIC_UUID = '00002201-0000-1000-8000-00805F9B34FB' 8

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。