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
|
||||
}
|
||||
Reference in New Issue
Block a user