deokgyuhan / flutter_ble_peripheral_central

MIT License
9 stars 6 forks source link

flutter_ble_peripheral_central

"flutter_ble_peripheral_central plugin project"

1. Introduction

2. Screenshots

Home BLE Central BLE Peripheral
Home BLE Central BLE Peripheral

3. Setup

Android

1) Add permission to your AndroidManifest.xml.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2) Register peripheral service to your AndroidManifest.xml.

    <service android:name="com.novice.flutter_ble_peripheral_central.ble.BlePeripheralService"
             android:exported="true"
             android:enabled="true"
             android:permission="android.permission.BLUETOOTH">
    </service>

3) Register central service to your AndroidManifest.xml.

    <service android:name="com.novice.flutter_ble_peripheral_central.ble.BleCentralService"
             android:exported="true"
             android:enabled="true"
             android:permission="android.permission.BLUETOOTH_ADMIN">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
        </intent-filter>
    </service>

Ios

4. Usage

import 'package:flutter_ble_peripheral_central/flutter_ble_peripheral_central.dart';

//plugin instance 
final  _flutterBlePeripheralCentralPlugin = FlutterBlePeripheralCentral();

//getPlatformVersion
var platformVersion = await _flutterBlePeripheralCentralPlugin.getPlatformVersion();

//peripheral mode
StreamSubscription<dynamic>? _eventSubscription;
var advertisingText = "advertising data";   //only ios
var readableText = "readable data";

//start advertising
_eventSubscription = await _flutterBlePeripheralCentralPlugin.startBlePeripheralService(advertisingText, readableText).listen((event) {
//handle event 
// ....
 });

//editTextCharForRead
await _flutterBlePeripheralCentralPlugin.editTextCharForRead(readableText);

//sendIndicate
var sendData = 'send data call';
var result = await _flutterBlePeripheralCentralPlugin.sendIndicate(sendData);

//stop advertising
await _flutterBlePeripheralCentralPlugin.stopBlePeripheralService();

//central mode
//scan and autoconnect
StreamSubscription<dynamic>? _eventSubscription;
_eventSubscription = await _flutterBlePeripheralCentralPlugin.scanAndConnect().listen((event) {
// handle event 
// ....
});

//disconnect
await _flutterBlePeripheralCentralPlugin.bleDisconnect();

//read characteristic
var result = await _flutterBlePeripheralCentralPlugin.bleReadCharacteristic();

//write characteristic
var sendData = 'send data';
await _flutterBlePeripheralCentralPlugin.bleWriteCharacteristic(sendData);

- Reference -

Table of UUIDs

Name UUID
Service 25AE1441-05D3-4C5B-8281-93D4E07420CF
Characteristic for read 25AE1442-05D3-4C5B-8281-93D4E07420CF
Characteristic for write 25AE1443-05D3-4C5B-8281-93D4E07420CF
Characteristic for indicate 25AE1444-05D3-4C5B-8281-93D4E07420CF

BLE Peripheral (all platforms)

Peripheral (also called Slave or Server) works similarly on all platforms:

Note 1: technically characteristics can have any amount of permissions (read, write default, write without response, notify, indicate), but in this project each characteristic has only one permission for simplicity.

Note 2: indication is a notification with response - Peripheral notifies, Central confirms that notification received.

BLE Central (all platforms)

Central (also called Master or Client) works similarly on all platforms: