Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.e_haji
- import android.bluetooth.BluetoothAdapter
- import android.bluetooth.BluetoothDevice
- import android.bluetooth.BluetoothServerSocket
- import android.bluetooth.BluetoothSocket
- import android.os.Bundle
- import android.os.Handler
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log
- import io.opencensus.stats.View
- import kotlinx.android.synthetic.main.activity_health.*
- import java.io.IOException
- import java.io.InputStream
- import java.io.OutputStream
- import java.util.*
- import java.util.jar.Attributes
- private val MY_UUID = UUID.fromString("5ec0fdd7-9d73-4da0-b086-544eca32289d")
- private val NAME = "E-HAJJ"
- val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
- private const val TAG = "MY_APP_DEBUG_TAG"
- const val MESSAGE_READ: Int = 0
- const val MESSAGE_WRITE: Int = 1
- const val MESSAGE_TOAST: Int = 2
- private lateinit var myHandler: Handler
- class healthmon : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_health)
- if (bluetoothAdapter?.isEnabled == false) {
- bluetoothAdapter.enable()
- }
- val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
- pairedDevices?.forEach { device ->
- val deviceName = device.name
- val deviceHardwareAddress = device.address // MAC address
- bluetoothNameTextView.text = deviceName
- bluetoothAddressTextView.text = deviceHardwareAddress
- connectToDeviceButton.setOnClickListener()
- {
- AcceptThread()
- ConnectThread(device)
- }
- }
- }
- private inner class AcceptThread : Thread() {
- private val mmServerSocket: BluetoothServerSocket? by lazy(LazyThreadSafetyMode.NONE) {
- bluetoothAdapter?.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID)
- }
- override fun run() {
- // Keep listening until exception occurs or a socket is returned.
- var shouldLoop = true
- while (shouldLoop) {
- val socket: BluetoothSocket? = try {
- mmServerSocket?.accept()
- } catch (e: IOException) {
- Log.e(TAG, "Socket's accept() method failed", e)
- shouldLoop = false
- null
- }
- socket?.also {
- ConnectedThread(it)
- mmServerSocket?.close()
- shouldLoop = false
- }
- }
- }
- // Closes the connect socket and causes the thread to finish.
- fun cancel() {
- try {
- mmServerSocket?.close()
- } catch (e: IOException) {
- Log.e(TAG, "Could not close the connect socket", e)
- }
- }
- }
- private inner class ConnectThread(device: BluetoothDevice) : Thread() {
- private val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
- device.createRfcommSocketToServiceRecord(MY_UUID)
- }
- public override fun run() {
- // Cancel discovery because it otherwise slows down the connection.
- bluetoothAdapter?.cancelDiscovery()
- mmSocket?.use { socket ->
- // Connect to the remote device through the socket. This call blocks
- // until it succeeds or throws an exception.
- socket.connect()
- // The connection attempt succeeded. Perform work associated with
- // the connection in a separate thread.
- ConnectedThread(socket)
- }
- }
- // Closes the client socket and causes the thread to finish.
- fun cancel() {
- try {
- mmSocket?.close()
- } catch (e: IOException) {
- Log.e(TAG, "Could not close the client socket", e)
- }
- }
- }
- private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {
- private val mmInStream: InputStream = mmSocket.inputStream
- private val mmOutStream: OutputStream = mmSocket.outputStream
- private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream
- override fun run() {
- var numBytes: Int // bytes returned from read()
- // Keep listening to the InputStream until an exception occurs.
- while (true) {
- // Read from the InputStream.
- numBytes = try {
- mmInStream.read(mmBuffer)
- } catch (e: IOException) {
- Log.d(TAG, "Input stream was disconnected", e)
- break
- }
- // Send the obtained bytes to the UI activity.
- val readMsg = myHandler.obtainMessage(MESSAGE_READ, numBytes, -1, mmBuffer)
- receivedMessageTextView.text = readMsg.toString()
- }
- }
- fun cancel() {
- try {
- mmSocket.close()
- } catch (e: IOException) {
- Log.e(TAG, "Could not close the connect socket", e)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement