Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.38 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3.     var bluetoothAdapter: BluetoothAdapter? = null
  4.     lateinit var bluetoothDevice: BluetoothDevice
  5.     val REQUEST_ENABLE_BLUETOOTH = 1
  6.  
  7.     companion object {
  8.         val EXTRA_ADRESS: String = "Device_adress"
  9.     }
  10.  
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         super.onCreate(savedInstanceState)
  13.         setContentView(R.layout.activity_main)
  14.  
  15.         bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
  16.  
  17.         val recyclerView = findViewById(R.id.recyclerView) as RecyclerView
  18.  
  19.         recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL,false)
  20.  
  21.         val devices = ArrayList<Device>()
  22.  
  23.         devices.add(Device("kalle","123",2))
  24.         devices.add(Device("kalle","123",2))
  25.         devices.add(Device("kalle","123",2))
  26.  
  27.         val adapter = CustomAdapter(devices)
  28.  
  29.         recyclerView.adapter = adapter
  30.  
  31.         if(bluetoothAdapter == null ){
  32.             toast("This device does not support bluetooth")
  33.             return
  34.         }
  35.         if(!bluetoothAdapter!!.isEnabled){
  36.             val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
  37.             startActivityForResult(enableBluetoothIntent,REQUEST_ENABLE_BLUETOOTH)
  38.         }
  39.  
  40.         val receiver = object : BroadcastReceiver() {
  41.             override fun onReceive(context: Context, intent: Intent) {
  42.                 val action = intent.getAction()
  43.                 Log.d("KALLE123","Action = " + action )
  44.             }
  45.         }
  46.  
  47.         val intentFilter: IntentFilter = IntentFilter(BluetoothDevice.ACTION_FOUND)
  48.         registerReceiver(receiver,intentFilter)
  49.  
  50.         if(bluetoothAdapter!!.isDiscovering) {
  51.             bluetoothAdapter!!.cancelDiscovery()
  52.         }
  53.         bluetoothAdapter!!.startDiscovery()
  54.  
  55.     }
  56.  
  57.  
  58.  
  59.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  60.         super.onActivityResult(requestCode, resultCode, data)
  61.  
  62.         if(requestCode == REQUEST_ENABLE_BLUETOOTH){
  63.             if(requestCode == Activity.RESULT_OK) {
  64.                 if(bluetoothAdapter!!.isEnabled){
  65.                     toast("Bluetooth enabled")
  66.                 }else{
  67.                     toast("bluetooth disabled")
  68.                 }
  69.             }
  70.         }else if(resultCode == Activity.RESULT_CANCELED){
  71.             toast("")
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement