yes, as discovered on the other thread, I have "fixed" the "checkFrom" handle to use the "wait with messages" syntax... that works fine now.
You are right (of course) why the "version" of "doNoACK" I presented doesn't work... I actually do know NOT to use a "message" handler as a "command"... stupid of me but I'm getting so frustrated at not being able to re-initialise the BLE card, I guess I was grasping at straws and didn't notice the type of handler when I referenced the script in the previous posting. I tried substituting "go to BLE_Card" in its place... and/or "go to this stack" ... but ...
To reiterate, the BLE card works PERFECTLY when initially opened from the very beginning of the app. Connects to the BLE device and (now) receives ACK's from the Arduino everytime I write to the BLE device... in fact it does it so well, I have to force the code to "think" it didn't receive an ACK. BUT...
Since the BLE_Card performs a "closeCard" action when I make it "go to <main card>", you would think that if I "go to BLE_Card" from my main card, that would COMPLETELY re-start the BLE connection... and it absolutely DOES NOT... and since I force the connection to stop with "mergBLECancelConnectionToPeripheral tPeripheral" (yes, I've double checked the "value" in the "tPeripheral" many, many times ... and this cancel command definitely stops the BLE connection), I would have thought THAT command would have "allowed" the "go to BLE_Card" to re-initialize... again.. not so.
I TRULY appreciate you assistance on this matter... however since the BLE_Card code is fairly long and DEFINITELY quite involved *AND* (most importantly) it works each time the app starts, I'm not sure that you wouldn't be wasting your time. Call it a gut feeling but I get the impression that somehow something gets "struck" in the mergBLE implementation and once a connection is established, it CANNOT be re-established without removing the ENTIRE stack from memory.. by stopping the app and returning to the iOS icon page (or whatever it's called... desktop?? .. dunno) and then calling the app again. ... however... I am including the whole BLE_Card script below.
I've contacted Monte (the original developer) and he wants me to post these details on the "MergEXT" form and send them to
support@livecode.com. I will do that... but .. please... don't give up on me. If, after reading this response, anything else comes to mind, I will ABSOLUTELY appreciate hearing it.
Cheers
Doug
NOTE** Most of this code is directly from the mergBLE example included in the LC 8 package... most of the fields have been left but made invisible on my version of the BLE Card to try to ensure I didn't "break" the original script. Most of the variables are used elsewhere .. I tend to "overuse" globals (for historic coding reasons) and copy all variable setups to all of the cards and most objects within my app... just my "development" methodology.
Code: Select all
global blinkID, BLEInit, BLESrc
global currentObj
global Dry
global isLower, isOpen, isUpper
global lowerPos
global maxFreq, maxQ, maxVol, maxWetDry, minFreq, MinMax, minQ, minVol, minWetDry, ModeStr, ModeVal
global preButton, Preset, PresetButton, PresetFile, PresetLine
global rectID
global saveID, sCentralA, skipDrag, skipLimit, skipLine, skipStop, skipUp, sPeripheralA, stackName, stopBlink, startBLE
global UpDn, upperPos
global vborderRef
global waitDelay, wborderRef, Wet, wetdry, WetDryValue
local altheConnections
local b, baseItem, buttonID
local c, calv, calx
local d, diffVal
local e
local f
local g
local h
local i
local makeInt
local rectPos, resultVal
local temp1, temp2, temp3, temp, thisMode, thisModeVal
local v
local w
local x
local y
local z
on openCard
if the platform <> "iphone" then
go to card "anycaster_card1"
end if
put empty into field "central state" of card "BLE_Card"
put empty into field "peripheral state" of card "BLE_Card"
put empty into field "service" of card "BLE_Card"
put empty into field "writable" of card "BLE_Card"
put empty into field "notify" of card "BLE_Card"
put empty into field "connections" of card "BLE_Card"
//answer "start initializing BLE"
//try // just incase the desktop build isn't installed
mergBLEInitialize
//end try
//answer "end initializing BLE"
end openCard
on closeCard
/*
mergBLEStopScanningForPeripherals
mergBLEStopAdvertising
mergBLEDeleteService fld "service" of card "BLE_Card"
*/
end closeCard
on mergBLEDidDiscoverPeripheral pPeripheral, pName, pRSSI
if (pName contains "AnyCaster") or (pName contains "Adafruit") then
put pName into sPeripheralA[pPeripheral]["name"]
put pRSSI into sPeripheralA[pPeripheral]["RSSI"]
mergBLEConnectPeripheral pPeripheral
end if
end mergBLEDidDiscoverPeripheral
on mergBLEDidFailToConnectPeripheral pPeripheral, pError
answer "Failed to connect to "&sPeripheralA[pPeripheral]["name"]&cr&pError
end mergBLEDidFailToConnectPeripheral
on mergBLEDidConnectPeripheral pPeripheral
updateConnections
mergBLEPeripheralDiscoverServices pPeripheral, fld "service"
end mergBLEDidConnectPeripheral
on mergBLEPeripheralDidDiscoverServices pPeripheral, pServices, pError
if pError is not empty then
answer "Error discovering services for "&sPeripheralA[pPeripheral]["name"] & cr & pError
else
if fld "service" is among the lines of pServices then
mergBLEPeripheralDiscoverCharacteristicsForService pPeripheral, fld "service" --, fld "characteristic"
end if
end if
end mergBLEPeripheralDidDiscoverServices
on mergBLEPeripheralDidDiscoverCharacteristicsForService pPeripheral, pService, pCharacteristics, pError
//answer "Go ahead?" with "Yes" or "No"
put "mergBLEPeripheralDidDiscoverCharacteristicsForService" && pPeripheral, pService, pCharacteristics, pError
if pError is not empty then
answer "Error discovering characteristics for "&sPeripheralA[pPeripheral]["name"] & cr & pError
else
repeat for each line tCharacteristic in pCharacteristics
/*
answer "ANSWER #3" & return & \
"pPeripheral: " & return & tab & pPeripheral & return && \
"pService: " & return & tab & pService & return & \
"pName:" & return & tab & sPeripheralA[pPeripheral]["name"] & return & \
"tCharacteristic: " & return & tab & tCharacteristic & return & \
"pValue: " & return & tab & pValue
*/
switch
case tCharacteristic is fld "notify" of card "BLE_Card"
mergBLEPeripheralSetNotificationsForCharacteristic pPeripheral, pService, fld "notify" of card "BLE_Card", true
break
case tCharacteristic is fld "writable" of card "BLE_Card"
--mergBLEPeripheralWriteValueForCharacteristic pPeripheral, pService, tCharacteristic, "Initialize stuff" & "?"
writeCharacteristic pService, tCharacteristic, "Initialize stuff"
put true into BLEInit
break
default
-- read our random writable characteristic
mergBLEPeripheralReadValueForCharacteristic pPeripheral, pService, tCharacteristic
break
end switch
end repeat
end if
put 0 into x
repeat until ((the currentFrame of image "cat_69.gif" of card "BLE_Card" > 11) or (x > 5000))
wait 10 milliseconds
add the currentFrame of image "cat_69.gif" of card "BLE_Card" to x
put x into line 2 of field "connections" of card "BLE_Card"
end repeat
go to card "anycaster_card1"
end mergBLEPeripheralDidDiscoverCharacteristicsForService
on mergBLEPeripheralDidUpdateValueForCharacteristic pPeripheral, pCharacteristic, pValue, pError
if pError is not empty then
answer "Error reading value for characteristic for "&sPeripheralA[pPeripheral]["name"] & cr & pError
else
/*
answer "ANSWER #2" & return & \
"pPeripheral: " & return & tab & pPeripheral & return & \
"pService: " & return & tab & pService & return & \
"pName:" & return & tab & sPeripheralA[pPeripheral]["name"] & return & \
"pCharacteristic: " & return & tab & pCharacteristic & return & \
"pValue: " & return & tab & pValue & return & \
"fld notify: " & return & tab & fld "notify" of card "BLE_Card" & return & \
"fld writable: " & return & tab & fld "writable" of card "BLE_Card"
*/
switch
case pCharacteristic is fld "notify" of card "BLE_Card"
--put true into BLESrc
put interpret_incoming(pValue) after field "debugField" of card "anycaster_card1"
break
case pCharacteristic is fld "writable"
answer "at Writable ... pCharacteristic: " & pCharacteristic & "... Should never get here ... check ANSWER #2"
break
default
answer "at Default ... pCharacteristic: " & pCharacteristic & " ... Should never get here ... check ANSWER #2"
go to card "anycaster_card1"
break
end switch
end if
end mergBLEPeripheralDidUpdateValueForCharacteristic
on mergBLEDidDisconnectPeripheral pUUID
updateConnections
end mergBLEDidDisconnectPeripheral
on updateConnections
repeat for each key tCentral in sCentralA
put "Central "& tCentral & return after theConnections
end repeat
repeat for each key tPeripheral in sPeripheralA
put "Peripheral "&sPeripheralA[tPeripheral]["name"] & return after theConnections
end repeat
put theConnections into fld "connections"
end updateConnections
on mergBLECentralManagerDidUpdateState pState
--answer pState
put pState into fld "central state" of card "BLE_Card"
if pState is "powered on" then
mergBLEScanForPeripheralsWithServices fld "service" of card "BLE_Card"
else
put empty into field "service" of card "BLE_Card"
put empty into field "writable" of card "BLE_Card"
put empty into field "notify" of card "BLE_Card"
put empty into field "connections" of card "BLE_Card"
end if
end mergBLECentralManagerDidUpdateState
on mergBLEPeripheralManagerDidUpdateState pState
--answer pState
put pState into fld "peripheral state" of card "BLE_Card"
if pState is "powered on" then
put "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" into pService
put pService into field "service" of card "BLE_Card"
mergBLECreateService fld "service" of card "BLE_Card"
put "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" into pCharacteristic
put pCharacteristic into field "writable" of card "BLE_Card"
mergBLEAddCharacteristicToService fld "service" of card "BLE_Card", fld "writable" of card "BLE_Card", false, true, true, false, false, false, false, false
put "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" into field "notify" of card "BLE_Card"
mergBLEAddCharacteristicToService fld "service" of card "BLE_Card", fld "notify" of card "BLE_Card", false, false, false, true, true, false, false, false
/*
answer field "service" of card "BLE_Card" & crlf & \
field "writable" of card "BLE_Card" & crlf & \
field "notify" of card "BLE_Card"
*/
else
put empty into field "service" of card "BLE_Card"
put empty into field "writable" of card "BLE_Card"
put empty into field "notify" of card "BLE_Card"
put empty into field "connections" of card "BLE_Card"
put empty into sPeripheralA
put empty into sCentralA
put empty into pCharacteristics
end if
end mergBLEPeripheralManagerDidUpdateState
/*
on writeCharacteristic pService, pCharacteristic, pValue
repeat for each key tPeripheral in sPeripheralA
answer "Card OUTPUT" & return & \
"tPeripheral: " & return & tab & tPeripheral & return && \
"pService: " & return & tab & pService & return & \
"pName:" & return & tab & sPeripheralA[tPeripheral]["name"] & return & \
"pCharacteristic: " & return & tab & pCharacteristic & return & \
"pValue: " & return & tab & pValue
mergBLEPeripheralWriteValueForCharacteristic tPeripheral, pService, pCharacteristic, pValue
end repeat
end writeCharacteristic
*/