|
|
@ -17,12 +17,12 @@ import java.util.*
|
|
|
|
* incoming connections, a thread for connecting with a device, and a
|
|
|
|
* incoming connections, a thread for connecting with a device, and a
|
|
|
|
* thread for performing data transmissions when connected.
|
|
|
|
* thread for performing data transmissions when connected.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
class BluetoothService(context: Context?) {
|
|
|
|
// Member fields
|
|
|
|
// Member fields
|
|
|
|
private var bluetoothManager: BluetoothManager? = null
|
|
|
|
private var bluetoothManager: BluetoothManager? = null
|
|
|
|
private var mContext: Context? = null
|
|
|
|
private var mContext: Context? = null
|
|
|
|
// private val mAdapter: BluetoothAdapter
|
|
|
|
// private val mAdapter: BluetoothAdapter
|
|
|
|
private val mHandler: Handler
|
|
|
|
// private val mHandler: Handler
|
|
|
|
private var mSecureAcceptThread: AcceptThread? = null
|
|
|
|
private var mSecureAcceptThread: AcceptThread? = null
|
|
|
|
private var mInsecureAcceptThread: AcceptThread? = null
|
|
|
|
private var mInsecureAcceptThread: AcceptThread? = null
|
|
|
|
private var mConnectThread: ConnectThread? = null
|
|
|
|
private var mConnectThread: ConnectThread? = null
|
|
|
@ -46,7 +46,7 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
mNewState = mState
|
|
|
|
mNewState = mState
|
|
|
|
|
|
|
|
|
|
|
|
// Give the new state to the Handler so the UI Activity can update
|
|
|
|
// Give the new state to the Handler so the UI Activity can update
|
|
|
|
mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1).sendToTarget()
|
|
|
|
// mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1).sendToTarget()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -151,11 +151,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
mConnectedThread!!.start()
|
|
|
|
mConnectedThread!!.start()
|
|
|
|
|
|
|
|
|
|
|
|
// Send the name of the connected device back to the UI Activity
|
|
|
|
// Send the name of the connected device back to the UI Activity
|
|
|
|
val msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
|
|
|
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
|
|
|
|
val bundle = Bundle()
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(Constants.DEVICE_NAME, device.name)
|
|
|
|
bundle.putString(Constants.DEVICE_NAME, device.name)
|
|
|
|
msg.data = bundle
|
|
|
|
// msg.data = bundle
|
|
|
|
mHandler.sendMessage(msg)
|
|
|
|
// mHandler.sendMessage(msg)
|
|
|
|
// Update UI title
|
|
|
|
// Update UI title
|
|
|
|
updateUserInterfaceTitle()
|
|
|
|
updateUserInterfaceTitle()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -210,11 +210,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private fun connectionFailed() {
|
|
|
|
private fun connectionFailed() {
|
|
|
|
// Send a failure message back to the Activity
|
|
|
|
// Send a failure message back to the Activity
|
|
|
|
val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
|
|
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
|
|
|
val bundle = Bundle()
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(Constants.TOAST, "Unable to connect device")
|
|
|
|
bundle.putString(Constants.TOAST, "Unable to connect device")
|
|
|
|
msg.data = bundle
|
|
|
|
// msg.data = bundle
|
|
|
|
mHandler.sendMessage(msg)
|
|
|
|
// mHandler.sendMessage(msg)
|
|
|
|
mState = STATE_NONE
|
|
|
|
mState = STATE_NONE
|
|
|
|
// Update UI title
|
|
|
|
// Update UI title
|
|
|
|
updateUserInterfaceTitle()
|
|
|
|
updateUserInterfaceTitle()
|
|
|
@ -228,11 +228,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private fun connectionLost() {
|
|
|
|
private fun connectionLost() {
|
|
|
|
// Send a failure message back to the Activity
|
|
|
|
// Send a failure message back to the Activity
|
|
|
|
val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
|
|
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
|
|
|
val bundle = Bundle()
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(Constants.TOAST, "Device connection was lost")
|
|
|
|
bundle.putString(Constants.TOAST, "Device connection was lost")
|
|
|
|
msg.data = bundle
|
|
|
|
// msg.data = bundle
|
|
|
|
mHandler.sendMessage(msg)
|
|
|
|
// mHandler.sendMessage(msg)
|
|
|
|
mState = STATE_NONE
|
|
|
|
mState = STATE_NONE
|
|
|
|
// Update UI title
|
|
|
|
// Update UI title
|
|
|
|
updateUserInterfaceTitle()
|
|
|
|
updateUserInterfaceTitle()
|
|
|
@ -434,8 +434,8 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
bytes = mmInStream!!.read(buffer)
|
|
|
|
bytes = mmInStream!!.read(buffer)
|
|
|
|
|
|
|
|
|
|
|
|
// Send the obtained bytes to the UI Activity
|
|
|
|
// Send the obtained bytes to the UI Activity
|
|
|
|
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
|
|
|
|
// mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
|
|
|
|
.sendToTarget()
|
|
|
|
// .sendToTarget()
|
|
|
|
} catch (e: IOException) {
|
|
|
|
} catch (e: IOException) {
|
|
|
|
// Log.e(TAG, "disconnected", e)
|
|
|
|
// Log.e(TAG, "disconnected", e)
|
|
|
|
connectionLost()
|
|
|
|
connectionLost()
|
|
|
@ -454,8 +454,8 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
mmOutStream!!.write(buffer)
|
|
|
|
mmOutStream!!.write(buffer)
|
|
|
|
|
|
|
|
|
|
|
|
// Share the sent message back to the UI Activity
|
|
|
|
// Share the sent message back to the UI Activity
|
|
|
|
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
|
|
|
|
// mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
|
|
|
|
.sendToTarget()
|
|
|
|
// .sendToTarget()
|
|
|
|
} catch (e: IOException) {
|
|
|
|
} catch (e: IOException) {
|
|
|
|
// Log.e(TAG, "Exception during write", e)
|
|
|
|
// Log.e(TAG, "Exception during write", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -520,6 +520,6 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|
|
|
bluetoothManager!!.adapter
|
|
|
|
bluetoothManager!!.adapter
|
|
|
|
mState = STATE_NONE
|
|
|
|
mState = STATE_NONE
|
|
|
|
mNewState = mState
|
|
|
|
mNewState = mState
|
|
|
|
mHandler = handler
|
|
|
|
// mHandler = handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|