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

outdated

A bluetooth driver plugin project.

Btlib #

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

Cross-Platform Bluetooth LE #

This plugin support both platforms (iOS and Android).

Getting Started #

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"; }

Example Code #

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

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

    /// start scan
    driver.startScan();

    /// waitting scan result...

    /// stop scan
    driver.stopScan();

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

    /// 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);

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

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

    /// 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
0
pub points
0%
popularity

Publisher

unverified uploader

A bluetooth driver plugin project.

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on btlib