-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt.cpp
61 lines (48 loc) · 1.57 KB
/
bt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "bt.h"
#include <TFT_eSPI.h>
#include <SPI.h>
#include "WiFi.h"
#include <Wire.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include "BLEDevice.h"
#include "BluetoothSerial.h"
#include "VescUart.h"
uint8_t vesc_bt_address[6] = {0x98, 0xD3, 0x31, 0x90, 0x5B, 0xC7}; // change to your HC-06 BT addres you can get it using BluetoothCL.exe from http://www.nirsoft.net
char * vesc_pin = "1234"; // change to your BT pin
bool BTConnected = false;
BluetoothSerial SerialBT;
VescUart VescConnection;
bool bt_connect(void)
{
SerialBT.enableSSP();
SerialBT.begin("ESP32test", true);
SerialBT.setPin(vesc_pin);
BTConnected = SerialBT.connect(vesc_bt_address);
if(BTConnected)
{
Serial.println("Connected Succesfully!");
VescConnection.setSerialPort(&SerialBT);
} else {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
return BTConnected;
}
void bt_scan(void)
{
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(true);
BLEScanResults scanResults = pBLEScan->start(30);
int count = scanResults.getCount();
String noOfDevicesStr = "No of devs:" + String(count);
Serial.println(noOfDevicesStr);
for (int i = 0; i < count; i++)
{
BLEAdvertisedDevice bleSensor = scanResults.getDevice(i);
String sensorName = bleSensor.getName().c_str();
String address = bleSensor.getAddress().toString().c_str();
Serial.println("Find" + address + " " + sensorName);
}
}