# **Android Studio**
## Implement Bluetooth and Mqtt protocol
---
### Classic Bluetooth
#### The Information of the Smart Insole
```java
public static final String SerialPortUUIDL="0000dfb1-0000-1000-8000-00805f9b34fb";
public static final String SerialPortUUIDR="0000dfb1-0000-1000-8000-00805f9b34fb";
public static final String SerialPortUUIDV="0000dfb1-0000-1000-8000-00805f9b34fb";
public static final String CommandUUIDL="0000dfb2-0000-1000-8000-00805f9b34fb";
public static final String CommandUUIDR="0000dfb2-0000-1000-8000-00805f9b34fb";
public static final String CommandUUIDV="0000dfb2-0000-1000-8000-00805f9b34fb";
public static final String ModelNumberStringUUIDL="00002a24-0000-1000-8000-00805f9b34fb";
public static final String ModelNumberStringUUIDR="00002a24-0000-1000-8000-00805f9b34fb";
public static final String ModelNumberStringUUIDV="00002a24-0000-1000-8000-00805f9b34fb";
```
#### Bluetooth Manager
:::success
```java
private BluetoothManager mBluetoothManagerL;
private BluetoothManager mBluetoothManagerR;
```
:::
#### Bluetooth Adapter
:::success
```java
private BluetoothAdapter mBluetoothAdapterL;
private BluetoothAdapter mBluetoothAdapterR;
```
:::
#### What is the device using below?
:::warning
```java
private BluetoothManager mBluetoothManagerV;
private BluetoothAdapter mBluetoothAdapterV;
```
:::
#### Initialize Bluetooth and check if the phone supports Bluetooth
```java
public boolean initializeR() {
// For API level 18 and above, get a reference to BluetoothAdapter through
// BluetoothManager.
System.out.println("BluetoothLeService initialize"+mBluetoothManagerR);
if (mBluetoothManagerR == null) {
mBluetoothManagerR = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManagerR == null) {
Log.e(TAG, "Unable to initialize BluetoothManager.");
return false;
}
}
mBluetoothAdapterR = mBluetoothManagerR.getAdapter();
if (mBluetoothAdapterR == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
return false;
}
return true;
}
```
#### Get Instance of BluetoothDevice
```java
final BluetoothDevice deviceL = mBluetoothAdapterL.getRemoteDevice(addressL);
if (deviceL == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
```
#### The Smart Insole communicates with classic Bluetooth, why does it use BLE connection?
:::warning
```java
synchronized(this)
{
mBluetoothGattR = deviceR.connectGatt(this, false, mGattCallbackR);
}
```
:::
#### Protocol of SmartInsole

##### Reference project
```java
private String start_bytes = "0 254 128";
private int max_data_len = 114;
private int ns_list[] = {58, 76, 77, 78, 79, 80, 81, 82, 83, 84, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 112, 113, 114};//不要的資料
```
:::warning
1.此專案為何有其他藍芽裝置進行連線
2.為何使用BLE Protocol,而非Classic Bt
:::
#### Implement Scheme
#### Classic Bluetooth

##### 從第三方藍芽APP得知,此SmartInsole運行在Classic Bt protocol
#### 檢查手機端是否支援藍芽協議
```java
public void CheckPhoneBt(Activity activity){
Context context = activity.getApplicationContext();
// bluetoothManager = context.getSystemService(BluetoothManager.class);
// bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(context, "The Device don't support bt", Toast.LENGTH_SHORT).show();
}//determine that device is support bluetooth
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
```
#### 以BluetoothAdapter 取得 遠端藍芽裝置 之 BluetoothDevice類資訊
```java
public BluetoothDevice getBtDevice(String mac){
bluetoothDeviceL = bluetoothAdapter.getRemoteDevice(mac); //取得遠端藍芽裝置 BluetoothDeviceL 類資料。
// Log.d(TAG, bluetoothDeviceL.getName());
// Log.d(TAG, bluetoothDeviceL.getAddress());
return bluetoothDeviceL;
}
```
#### Create socket between app and remote devices.
```java
private BluetoothSocket CreatSocket(BluetoothDevice device){
BluetoothSocket tmp = null;
try{
tmp = device.createRfcommSocketToServiceRecord(SerialPortUUID_L);
}catch (IOException e){
Log.e("Connect_socket", "Socket's create() method failed",e);
}
return tmp;
}//CreatSocket()
```
#### Connecting socket with remote device
```java
private boolean btConnectSocket(BluetoothSocket mmSocket){
bluetoothAdapter.cancelDiscovery();
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
Log.d(TAG, "client socket connected");
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mmSocket.close();
} catch (IOException closeException) {
Log.e(TAG, "Could not close the client socket", closeException);
}
return false;
}
if(mmSocket.isConnected() == true){
Log.d(TAG, "build socket sucess");
return true;
// BtSerialThread btSerialThread = new BtSerialThread(mmSocket, "SmartInsole_L");
//
// btSerialThread.write(end_cmd_byte);
// btSerialThread.write(start_cmd_byte);
// btSerialThread.run();
}
return false;
}//btConnectSocket()
```
#### Finally, generator a thread to handle bt communication
#### Android Studio Bluetooth 進度
1. Creating and building socket between app and smart insole through classic bluetooth. 完成
2. Read and collect data through the socket.


#### Android Studio and FastAPI MQTT進度
1. transmit data through Topic between app and server. 已完成.
2. 將手機讀取到的足底壓力資料透過MQTT傳輸資料至FastAPI Server做處理.進行中
#### Android GUI 移植 (0%)

#### 與組員討論如何部署Model至FastAPI or App
---
#### 壓力值的分布