btlib 0.1.1 copy "btlib: ^0.1.1" to clipboard
btlib: ^0.1.1 copied to clipboard

A bluetooth driver plugin project, using a easy event-driven way. Support both platforms (iOS and Android).

Btlib #

A bluetooth driver plugin project, using a easy event-driven way.

Cross-Platform Bluetooth LE #

This plugin support both platforms (iOS and Android).

Example Code #

class BTUUID {
  static const String TX_SERV       = "1802";
  static const String TX_CHAR       = "2A06";
  static const String RX_SERV       = "180F";
  static const String RX_CHAR       = "2A19";
  static const String NOTIFY_SERV   = "FFE0";
  static const String NOTIFY_CHAR   = "FFE1";
}

main() {
    /// get instance
    Btlib driver = Btlib();

    /// listen to events
    StreamSubscription subscription;
    subscription = driver.listen(onEvent);

    /// start scan
    driver.startScan();

    /// waitting scan result...
    /// get device address...

    /// stop scan
    driver.stopScan();

    /// connect to device
    driver.connect(address);

    /// waitting connect result...
    /// get device services & characteristics ...

    /// write characteristic
    driver.writeCharacteristic(
        address,
        BTUUID.TX_SERV, 
        BTUUID.TX_CHAR, 
        Uint8List.fromList([0,1,2,3]));

    /// read characteristic
    driver.readCharacteristic(
        address,
        BTUUID.RX_SERV, 
        BTUUID.RX_CHAR);

    /// wait data available...

    /// set notification
    driver.setCharacteristicNotification(
        address,
        BTUUID.NOTIFY_SERV, 
        BTUUID.NOTIFY_CHAR, 
        true);

    /// wait data available (notification)...

    /// get rssi
    driver.getRSSI(address);

    /// wait rssi value...

    /// desconnect device
    driver.disconnect(address);
}
/*
events:
    BTEvent.BLE_START_SCAN               
    BTEvent.BLE_STOP_SCAN                   
    BTEvent.BLE_SCAN_RESULT                 /// return name,address,rssi       
    BTEvent.BLE_CONNECTED                   /// return address 
    BTEvent.BLE_DISCONNECTED                /// return address
    BTEvent.BLE_SERVICES_CHARACTERISTICS    /// return address,service,characteristic
    BTEvent.BLE_DATA_AVAILABLE              /// return address,service,characteristic,data
    BTEvent.BLE_RSSI                        /// return address,rssi             
*/
void onEvent(Object event) {
    HashMap<String, Object> map = HashMap.from(event);
    print(map);
    if (map["event"] == BTEvent.BLE_START_SCAN) {
        print( "start scan" );
    } else if (map["event"] == BTEvent.BLE_STOP_SCAN) {
        print( "stop scan" );
    } else if (map["event"] == BTEvent.BLE_SCAN_RESULT) {
        print( map["name"] );
        print( map["address"] );
        print( map["rssi"] );
    } else if (map["event"] == BTEvent.BLE_CONNECTED) {
        print( map["address"] );
    } else if (map["event"] == BTEvent.BLE_DISCONNECTED) {
        print( map["address"] );
    } else if (map["event"] == BTEvent.BLE_SERVICES_CHARACTERISTICS) {
        print( map["address"] );
        print( map["service"] );
        print( map["characteristic"] );
    } else if (map["event"] == BTEvent.BLE_DATA_AVAILABLE) {
        print( map["address"] );
        print( map["service"] );
        print( map["characteristic"] );
        print( map["data"] );
    } else if (map["event"] == BTEvent.BLE_RSSI) {
        print( map["address"] );
        print( map["rssi"] );
    }
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A bluetooth driver plugin project, using a easy event-driven way. Support both platforms (iOS and Android).

Homepage

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on btlib