ip_global_ble 0.0.9 copy "ip_global_ble: ^0.0.9" to clipboard
ip_global_ble: ^0.0.9 copied to clipboard

IP_global_BLE

IP_global_BLE for Flutter #

A Flutter plugin that provides a BLE scanning

Usage #

Set android below permissions:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"  />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"  />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  1. Define at the top in main.dart
final GlobalKey<_HomeScreenState> glbKey = GlobalKey<_HomeScreenState>();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(key: glbKey), // Attach the GlobalKey, dont forget this.
    );
  }
}
  1. You can use library by declaring the callback functions:
void IP_CB_BLE_ShowScanInfo(String msg) {
  setState(() {
    msg1=msg;
    print("IP_CB_BLE_ShowScanInfo : " + msg);
  });
}
void IP_CB_BLE_ScannedBLEList( lstScannedBLE) {
  setState(() {
    print("IP_CB_BLE_ScannedBLEList : " + lstScannedBLE.length.toString());
  });
}

void IP_CB_BLE_BLEConnectInfo( String msg) {
  setState(() {
    msg1=msg;
    print("IP_CB_BLE_BLEConnectInfo : " + msg);
  });
}
void IP_CB_BLE_WriteBLEResponse( List<clsWriteBLEResponse> objlstResponse) {
  setState(() {
    msg1=objlstResponse.last.strOriginalRequest.toString() + " = " + objlstResponse.last.strResponse.toString();
    print("IP_CB_BLE_WriteBLEResponse : " + msg1);
  });
}
  1. Call IP_scanBLE with globalKey
IP_scanBLE(context, glbKey, "<Filter with Name>", null);
  1. Connect and Write/Read the data
List<String> lstWrite = [];
List<clsWriteBLEResponse> lstRead = [];
lstWrite.add("RSSIFROM:-100:");
lstWrite.add("RSSITO:-100:");
clsBLEConnect obj = new clsBLEConnect(
strServiceUUID:
"<ServiceUUID>",
strCharacteristicUUID:
"<CharacteristicUUID>",
lstRequest: lstWrite,
lstResponse: lstRead,
flgRequestWithoutResponse: false,
strMAC: strBLEMACAddress);
IP_scanBLE(context, glbKey, "<Filter with Name>", obj);
  1. Stop Scanning
IP_stopScanning();

On read data from GATT you will find the response in below event IP_CB_BLE_WriteBLEResponse class.

Events: #

IP_CB_BLE_ShowScanInfo:

Requesting Location Permission
Requesting Bluetooth Permission
Scanning...
<DeviceMAC>: <DeviceName> found!
Scanning Stopped...

IP_CB_BLE_ScannedBLEList:

class clsScannedBLE {
String name;
String BLEmac;
String WiFimac;
int rssi;

clsScannedBLE({
required this.name,
required this.BLEmac,
required this.WiFimac,
required this.rssi,
});
}

The event returns the class object on new entry

List<clsScannedBLE> lstScannedBLE

IP_CB_BLE_BLEConnectInfo:

Connecting...
Discovering Services
Discovering Characteristics
Writing WOR/WR <DATA>
Configuration # Successfully - You can replace # with Read / Write

IP_CB_BLE_WriteBLEResponse:

class clsWriteBLEResponse {
String strOriginalRequest;
String strResponse;

clsWriteBLEResponse({
required this.strOriginalRequest,
required this.strResponse,
});
}

The event returns the class object of clsWriteBLEResponse

List<clsWriteBLEResponse> lstResponse = [];

API Changes #