Last active
March 6, 2019 03:59
-
-
Save Ryderpro/3830d1558ba5cb60471f204bc989d80f to your computer and use it in GitHub Desktop.
Beat IoT device. Start, pause, and stop music playing iPhone with a PuckJS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Custom Beat IoT device. Start, pause, and stop music playing iPhone with a PuckJS. | |
This source code is an alternative to BLE HID. BLE HID is great and offers devices | |
a Bluetooth integration that lets a device simulate media, keyboard, mouse buttons. | |
Unfortunately, iPhones do not support BLE HID at the system level that is required, | |
but your macOS laptop or android device can. More on BLE HID here - | |
https://learn.sparkfun.com/tutorials/bluetooth-basics/bluetooth-profiles | |
This device intends to operate as a faux BLE HID through "Diny" [an iOS app]. | |
Load JS source code on device here https://www.espruino.com/ide/ | |
https://www.puck-js.com/ | |
*/ | |
NRF.setServices({ | |
0x180F : [Puck.getBatteryPercentage()], // Set battery level | |
0xBCDE : { // Set button state 0 play-pause, 1 play-pause, 2 stop | |
0xABCD : { | |
value : [0], | |
readable : true, | |
notify: true, | |
indicate : true, | |
broadcast : true | |
} | |
} | |
},{ advertise: [ 'BCDE' ] }); | |
var playFlag = true; // Redundancy - app play-pause will return 0 or 1 in addition to a BLE notify alert. If you don't trust the BLE alert, check if play-pause changed from 0 to 1. | |
setWatch(function(e) { | |
var len = e.time - e.lastTime; | |
if (len > 0.4) { | |
// advertise stop-restart | |
advertise(2); | |
digitalPulse(LED1,1,100); | |
} else { | |
// advertise play-pause | |
advertise(playFlag >>> 0); | |
playFlag =! playFlag; | |
digitalPulse(LED2,1,100); | |
} | |
}, BTN, { edge:"falling",repeat:true,debounce:50}); | |
function advertise(State){ | |
print(State); | |
NRF.updateServices({ | |
0xBCDE : { | |
0xABCD : { | |
value : [State], | |
notify: true | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment