Advertisement
FiqriZurani

BLE CONNECTION TO THE ANDROID APPS

Apr 18th, 2019
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.52 KB | None | 0 0
  1. package com.example.e_haji
  2.  
  3.  
  4. import android.bluetooth.BluetoothAdapter
  5. import android.bluetooth.BluetoothDevice
  6. import android.bluetooth.BluetoothServerSocket
  7. import android.bluetooth.BluetoothSocket
  8. import android.os.Bundle
  9. import android.os.Handler
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log
  12. import io.opencensus.stats.View
  13. import kotlinx.android.synthetic.main.activity_health.*
  14. import java.io.IOException
  15. import java.io.InputStream
  16. import java.io.OutputStream
  17. import java.util.*
  18. import java.util.jar.Attributes
  19.  
  20. private val MY_UUID = UUID.fromString("5ec0fdd7-9d73-4da0-b086-544eca32289d")
  21. private val NAME = "E-HAJJ"
  22. val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
  23. private const val TAG = "MY_APP_DEBUG_TAG"
  24. const val MESSAGE_READ: Int = 0
  25. const val MESSAGE_WRITE: Int = 1
  26. const val MESSAGE_TOAST: Int = 2
  27. private lateinit var myHandler: Handler
  28.  
  29. class healthmon : AppCompatActivity() {
  30.     override fun onCreate(savedInstanceState: Bundle?) {
  31.         super.onCreate(savedInstanceState)
  32.         setContentView(R.layout.activity_health)
  33.         if (bluetoothAdapter?.isEnabled == false) {
  34.             bluetoothAdapter.enable()
  35.         }
  36.         val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
  37.         pairedDevices?.forEach { device ->
  38.             val deviceName = device.name
  39.             val deviceHardwareAddress = device.address // MAC address
  40.             bluetoothNameTextView.text = deviceName
  41.             bluetoothAddressTextView.text = deviceHardwareAddress
  42.             connectToDeviceButton.setOnClickListener()
  43.             {
  44.                 AcceptThread()
  45.                 ConnectThread(device)
  46.             }
  47.         }
  48.     }
  49.     private inner class AcceptThread : Thread() {
  50.  
  51.         private val mmServerSocket: BluetoothServerSocket? by lazy(LazyThreadSafetyMode.NONE) {
  52.             bluetoothAdapter?.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID)
  53.         }
  54.  
  55.         override fun run() {
  56.             // Keep listening until exception occurs or a socket is returned.
  57.             var shouldLoop = true
  58.             while (shouldLoop) {
  59.                 val socket: BluetoothSocket? = try {
  60.                     mmServerSocket?.accept()
  61.                 } catch (e: IOException) {
  62.                     Log.e(TAG, "Socket's accept() method failed", e)
  63.                     shouldLoop = false
  64.                     null
  65.                 }
  66.                 socket?.also {
  67.                     ConnectedThread(it)
  68.                     mmServerSocket?.close()
  69.                     shouldLoop = false
  70.                 }
  71.             }
  72.         }
  73.  
  74.         // Closes the connect socket and causes the thread to finish.
  75.         fun cancel() {
  76.             try {
  77.                 mmServerSocket?.close()
  78.             } catch (e: IOException) {
  79.                 Log.e(TAG, "Could not close the connect socket", e)
  80.             }
  81.         }
  82.     }
  83.  
  84.     private inner class ConnectThread(device: BluetoothDevice) : Thread() {
  85.  
  86.         private val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
  87.             device.createRfcommSocketToServiceRecord(MY_UUID)
  88.         }
  89.  
  90.         public override fun run() {
  91.             // Cancel discovery because it otherwise slows down the connection.
  92.             bluetoothAdapter?.cancelDiscovery()
  93.  
  94.             mmSocket?.use { socket ->
  95.                 // Connect to the remote device through the socket. This call blocks
  96.                 // until it succeeds or throws an exception.
  97.                 socket.connect()
  98.  
  99.                 // The connection attempt succeeded. Perform work associated with
  100.                 // the connection in a separate thread.
  101.                 ConnectedThread(socket)
  102.             }
  103.         }
  104.  
  105.         // Closes the client socket and causes the thread to finish.
  106.         fun cancel() {
  107.             try {
  108.                 mmSocket?.close()
  109.             } catch (e: IOException) {
  110.                 Log.e(TAG, "Could not close the client socket", e)
  111.             }
  112.         }
  113.     }
  114.             private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {
  115.  
  116.                 private val mmInStream: InputStream = mmSocket.inputStream
  117.                 private val mmOutStream: OutputStream = mmSocket.outputStream
  118.                 private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream
  119.                 override fun run() {
  120.                     var numBytes: Int // bytes returned from read()
  121.  
  122.                     // Keep listening to the InputStream until an exception occurs.
  123.                     while (true) {
  124.                         // Read from the InputStream.
  125.                         numBytes = try {
  126.                             mmInStream.read(mmBuffer)
  127.                         } catch (e: IOException) {
  128.                             Log.d(TAG, "Input stream was disconnected", e)
  129.                             break
  130.                         }
  131.                         // Send the obtained bytes to the UI activity.
  132.                         val readMsg = myHandler.obtainMessage(MESSAGE_READ, numBytes, -1, mmBuffer)
  133.                         receivedMessageTextView.text = readMsg.toString()
  134.                     }
  135.  
  136.                 }
  137.                 fun cancel() {
  138.                     try {
  139.                         mmSocket.close()
  140.                     } catch (e: IOException) {
  141.                         Log.e(TAG, "Could not close the connect socket", e)
  142.                     }
  143.                 }
  144.             }
  145.  
  146.  
  147.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement