simulator v1
This commit is contained in:
48
internal/ble/service.go
Normal file
48
internal/ble/service.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package ble
|
||||
|
||||
import (
|
||||
"ble_simulator/internal/device"
|
||||
"log"
|
||||
|
||||
"tinygo.org/x/bluetooth"
|
||||
)
|
||||
|
||||
// SetupService configures the GATT service with command and notify characteristics
|
||||
func SetupService(adapter *bluetooth.Adapter, state *device.DeviceState) (*bluetooth.Characteristic, error) {
|
||||
var notifyChar bluetooth.Characteristic
|
||||
|
||||
err := adapter.AddService(&bluetooth.Service{
|
||||
UUID: ServiceUUID,
|
||||
Characteristics: []bluetooth.CharacteristicConfig{
|
||||
{
|
||||
UUID: CommandCharUUID,
|
||||
Flags: bluetooth.CharacteristicWritePermission |
|
||||
bluetooth.CharacteristicWriteWithoutResponsePermission,
|
||||
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
|
||||
cmd := string(value)
|
||||
log.Printf("[RX] Command: %s", cmd)
|
||||
|
||||
response := state.ProcessCommand(cmd)
|
||||
log.Printf("[TX] Response: %s", response)
|
||||
|
||||
_, err := notifyChar.Write([]byte(response + "\n"))
|
||||
if err != nil {
|
||||
log.Printf("[ERR] Failed to send response: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
UUID: NotifyCharUUID,
|
||||
Flags: bluetooth.CharacteristicReadPermission |
|
||||
bluetooth.CharacteristicNotifyPermission,
|
||||
Handle: ¬ifyChar,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ¬ifyChar, nil
|
||||
}
|
||||
28
internal/ble/uuids.go
Normal file
28
internal/ble/uuids.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package ble
|
||||
|
||||
import "tinygo.org/x/bluetooth"
|
||||
|
||||
// Nordic UART Service (NUS) UUIDs - same as real V-BLACK sensor
|
||||
// This allows the Flutter app to discover the simulator using the same scan filter
|
||||
var (
|
||||
// ServiceUUID - Nordic UART Service
|
||||
// 6e400001-b5a3-f393-e0a9-e50e24dcca9e
|
||||
ServiceUUID = bluetooth.NewUUID([16]byte{
|
||||
0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93,
|
||||
0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e,
|
||||
})
|
||||
|
||||
// CommandCharUUID - NUS RX (Write from app's perspective)
|
||||
// 6e400002-b5a3-f393-e0a9-e50e24dcca9e
|
||||
CommandCharUUID = bluetooth.NewUUID([16]byte{
|
||||
0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93,
|
||||
0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e,
|
||||
})
|
||||
|
||||
// NotifyCharUUID - NUS TX (Notify to app)
|
||||
// 6e400003-b5a3-f393-e0a9-e50e24dcca9e
|
||||
NotifyCharUUID = bluetooth.NewUUID([16]byte{
|
||||
0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93,
|
||||
0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e,
|
||||
})
|
||||
)
|
||||
Reference in New Issue
Block a user