flutter_blue_plusは、Bluetooth Low Energy (BLE) 専用のライブラリです。そのため、Bluetooth Classic デバイスのスキャンや接続には対応していません。Bluetooth Classicデバイスの名前や情報を表示するためには、別のアプローチが必要です。
Bluetooth ClassicとBLEの違い
-
Bluetooth Classic:
- 主に音声ストリーミング(ヘッドセットやスピーカー)やファイル転送に使用されます。
- デバイスのスキャンや接続には、Androidの
BluetoothAdapterやiOSのCoreBluetoothを使用します。
-
Bluetooth Low Energy (BLE):
- 低消費電力で、センサーやIoTデバイスとの通信に使用されます。
flutter_blue_plusはBLE専用のライブラリです。
Bluetooth Classicデバイスを扱う方法
Bluetooth Classicデバイスを扱うためには、以下の方法があります。
1. Android専用のAPIを使用する
Androidでは、BluetoothAdapterを使用してBluetooth Classicデバイスのスキャンや接続を行います。FlutterからAndroidのネイティブコードを呼び出すために、platform channelsを使用します。
以下は、AndroidでBluetooth Classicデバイスをスキャンする例です。
Androidネイティブコード (Java/Kotlin)
// MainActivity.java
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.app.FlutterActivity;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "bluetooth_channel";
private BluetoothAdapter bluetoothAdapter;
private MethodChannel.Result scanResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("startScan")) {
startScan(result);
} else {
result.notImplemented();
}
}
}
);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}
private void startScan(MethodChannel.Result result) {
scanResult = result;
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceAddress = device.getAddress();
scanResult.success(deviceName + " - " + deviceAddress);
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Flutter側のコード
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: BluetoothClassicScreen(),
);
}
}
class BluetoothClassicScreen extends StatefulWidget {
_BluetoothClassicScreenState createState() => _BluetoothClassicScreenState();
}
class _BluetoothClassicScreenState extends State<BluetoothClassicScreen> {
static const platform = MethodChannel('bluetooth_channel');
String _deviceInfo = 'No device found';
void _startScan() async {
try {
final String result = await platform.invokeMethod('startScan');
setState(() {
_deviceInfo = result;
});
} on PlatformException catch (e) {
setState(() {
_deviceInfo = "Failed to start scan: '${e.message}'.";
});
}
}
Widget build(BuildContextContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bluetooth Classic Scanner'),
),
body: Center(
child: Text(_deviceInfo),
),
floatingActionButton: FloatingActionButton(
onPressed: _startScan,
child: Icon(Icons.search),
),
);
}
}
2. iOS専用のAPIを使用する
iOSでは、CoreBluetoothを使用してBluetooth Classicデバイスを扱います。ただし、iOSはBluetooth Classicデバイスのスキャンに制限があるため、完全な機能を実装するのは難しい場合があります。
3. Flutterプラグインを使用する
Bluetooth Classicデバイスを扱うためのFlutterプラグインがいくつか存在します。以下はその例です。
-
flutter_bluetooth_serial: Bluetooth Classicデバイスとのシリアル通信をサポートします。
-
flutter_blue: BLEとBluetooth Classicの両方をサポートするプラグインです(ただし、Bluetooth Classicのサポートは限定的です)。
まとめ
flutter_blue_plusはBLE専用のため、Bluetooth Classicデバイスには対応していません。- Bluetooth Classicデバイスを扱うためには、Androidの
BluetoothAdapterやiOSのCoreBluetoothを使用する必要があります。 - Flutterプラグイン(例:
flutter_bluetooth_serial)を使用することで、Bluetooth Classicデバイスを簡単に扱える場合があります。
Bluetooth Classicデバイスの名前を表示するためには、上記のいずれかの方法を試してください。
