'Easy Bluetooth Interface 'Copyright Anthony Myatt 2009 CON StartDelimiter = $02 ' "STX" EndDelimiter = $03 ' "ETX" REQ_PACKET = $52 ' "R" CFM_PACKET = $43 ' "C" IND_PACKET = $69 ' "i" RES_PACKET = $72 ' "r" GAP_INQUIRY = $00 ' Initiates a search for other Bluetooth devices GAP_DEVICE_FOUND = $01 ' Indicates that a device has been found GAP_REMOTE_DEVICE_NAME = $02 ' Request the user-friendly name from a known remote Bluetooth device GAP_READ_LOCAL_NAME = $03 ' Request the user-friendly name for the local Bluetooth device GAP_WRITE_LOCAL_NAME = $04 ' Change the user-friendly name for the local Bluetooth device GAP_READ_LOCAL_BDA = $05 ' Read the Bluetooth device address of the local Bluetooth device GAP_SET_SCANMODE = $06 ' Change the Bluetooth scan mode GAP_GET_FIXED_PIN = $16 ' Reads the current fixed pin code from NVS GAP_SET_FIXED_PIN = $17 ' Stores a new fixed pin code in NVS GAP_GET_PIN = $75 ' This command is used to send a PIN code to the RBT-001 module as response to a GAP_GET_PIN indication GAP_GET_SECURITY_MODE = $18 ' GAP_SET_SECURITY_MODE = $19 GAP_REMOVE_PAIRING = $1B GAP_LIST_PAIRED_DEVICES = $1C GAP_ENTER_SNIFF_MODE = $21 GAP_EXIT_SNIFF_MODE = $37 GAP_ENTER_PARK_MODE = $38 GAP_EXIT_PARK_MODE = $39 GAP_ENTER_HOLD_MODE = $3A GAP_SET_LINK_POLICY = $3B GAP_GET_LINK_POLICY = $3C GAP_POWER_SAVE_MODE_CHANGED = $3D GAP_ACL_ESTABLISHED = $50 GAP_ACL_TERMINATED = $51 SPP_SET_PORT_CONFIG = $07 SPP_GET_PORT_CONFIG = $08 SPP_PORT_CONFIG_CHANGED = $09 SPP_ESTABLISH_LINK = $0A SPP_LINK_ESTABLISHED = $0B SPP_INCOMMING_LINK_ESTABLISHED = $0C SPP_RELEASE_LINK = $0D SPP_LINK_RELEASED = $0E SPP_SEND_DATA = $0F SPP_INCOMING_DATA = $10 SPP_TRANSPARENT_MODE = $11 SPP_CONNECT_DEFAULT_CON = $12 SPP_STORE_DEFAULT_CON = $13 SPP_GET_LIST_DEFAULT_CON = $14 SPP_DELETE_DEFAULT_CON = $15 SPP_SET_LINK_TIMEOUT = $57 SPP_GET_LINK_TIMEOUT = $58 SPP_PORT_STATUS_CHANGED = $3E SPP_GET_PORT_STATUS = $40 SPP_PORT_SET_DTR = $41 SPP_PORT_SET_RTS = $42 SPP_PORT_BREAK = $43 SPP_PORT_OVERRUN_ERROR = $44 SPP_PORT_PARITY_ERROR = $45 SPP_PORT_FRAMING_ERROR = $46 SDAP_CONNECT = $32 SDAP_DISCONNECT = $33 SDAP_CONNECTION_LOST = $34 SDAP_SERVICE_BROWSE = $35 SDAP_SERVICE_SEARCH = $36 SDAP_SERVICE_REQUEST = $1E SDAP_ATTRIBUTE_REQUEST = $3F CHANGE_NVS_UART_SPEED = $23 CHANGE_UART_SETTINGS = $48 SET_PORTS_TO_OPEN = $22 GET_PORTS_TO_OPEN = $1F RESTORE_FACTORY_SETTINGS = $1A STORE_CLASS_OF_DEVICE = $28 FORCE_MASTER_ROLE = $1D READ_OPERATION_MODE = $49 WRITE_OPERATION_MODE = $4A SET_DEFAULT_LINK_POLICY = $4C GET_DEFAULT_LINK_POLICY = $4D SET_EVENT_FILTER = $4E GET_EVENT_FILTER = $4F SET_DEFAULT_LINK_TIMEOUT = $55 GET_DEFAULT_LINK_TIMEOUT = $56 SET_DEFAULT_LINK_LATENCY = $63 GET_DEFAULT_LINK_LATENCY = $64 SET_PCM_SLAVE_CONFIG = $74 ENABLE_SDP_RECORD = $29 DELETE_SDP_RECORDS = $2A STORE_SDP_RECORD = $31 RESET = $26 READY = $25 TEST_MODE = $24 WRITE_ROM_PATCH = $47 READ_RSSI = $20 RF_TEST_MODE = $4B DISABLE_TL = $52 TL_ENABLED = $53 AWAIT_INITIALIZATION_EVENT = $66 ENTER_BLUETOOTH_MODE = $67 READ_NVS = $72 WRITE_NVS = $73 VAR Byte dataSet[340], rxBuffer[16], msgBuffer[254] Long Started OBJ serial : "Full_Serial" Pub Start (RXpin, TXPin, Baud) : okay | ptr Started := serial.start(RXpin, TXpin, 0, Baud) if Started == -1 return Started else Started := true return Started PUB Stop serial.stop Started := false PUB calculate_crc | crc crc := 0 crc := dataSet[1] + dataSet[2] + dataSet[3] + dataSet[4] crc := crc & $ff dataSet[5] := crc PUB receive_response | read_data, num_bytes, i num_bytes := 0 i := 0 repeat num_bytes++ read_data[i] := serial.rx i++ while (serial.rxcheck > 0) return read_data PUB send_cmd_receive_response(size) | crc, i calculate_crc repeat i from 0 to size serial.tx(dataSet[i]) waitcnt(clkfreq * 3 / 10 + cnt) return receive_response PUB sendREQ (opCode, msg, length) | ptr, dataLen if Started dataLen := length + 1 ptr := 0 dataSet[ptr++] := StartDelimiter dataSet[ptr++] := REQ_PACKET dataSet[ptr++] := opCode dataSet[ptr++] := dataLen dataSet[ptr++] := dataLen >> 8 dataSet[ptr++] := $00 Repeat strsize(msg) ' Repeat for the length of the message dataSet[ptr++] := byte[msg++] dataSet[ptr++] := EndDelimiter send_cmd_receive_response(ptr + 1) PUB Set_Local_Name (name) | len, ptr, i len := strsize(name) msgBuffer[0] := strsize(name) Repeat ptr from 1 to len + 1 msgBuffer[ptr] := byte[name][ptr - 1] sendREQ(GAP_WRITE_LOCAL_NAME, @msgBuffer, len)