zoukankan      html  css  js  c++  java
  • Android 6.0 Kotlin 蓝牙扫描

    package com.arci.myapplication

    import android.app.Activity
    import android.os.Bundle
    import android.support.design.widget.Snackbar
    import android.support.v7.app.AppCompatActivity
    import android.view.Menu
    import android.view.MenuItem
    import android.view.View

    import kotlinx.android.synthetic.main.activity_main.*
    import android.widget.TextView
    import kotlinx.android.synthetic.main.content_main.*
    import android.bluetooth.BluetoothManager
    import android.content.Context
    import android.bluetooth.BluetoothAdapter
    import android.widget.Toast
    import android.content.Intent
    import android.text.method.TextKeyListener.clear
    import android.bluetooth.BluetoothDevice
    import android.bluetooth.le.BluetoothLeScanner
    import android.content.BroadcastReceiver
    import android.content.IntentFilter

    class MainActivity : AppCompatActivity() {
    val REQUEST_BLUETOOTH_TURN_ON = 1
    lateinit var bluetoothAdapter: BluetoothAdapter
    lateinit var bluetoothManager: BluetoothManager

    var bluetoothDeviceList = ArrayList<BluetoothDevice>()

    // 广播接收发现蓝牙设备
    private val mReceiver = object : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
    val action = intent.action

    if (BluetoothAdapter.ACTION_DISCOVERY_STARTED == action) {
    //Log.d(FragmentActivity.TAG, "开始扫描...")
    Toast.makeText(context.applicationContext, "蓝牙扫描开始", Toast.LENGTH_SHORT).show()
    }

    if (BluetoothDevice.ACTION_FOUND == action) {
    val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
    if (device != null) {
    // 添加到ListView的Adapter。
    //mAdapter.add("设备名:" + device.name + " 设备地址:" + device.address)
    //mAdapter.notifyDataSetChanged()
    Toast.makeText(context.applicationContext, device.name + ": " + device.address, Toast.LENGTH_SHORT).show()
    }
    }

    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) {
    //Log.d(FragmentActivity.TAG, "扫描结束.")
    Toast.makeText(context.applicationContext, "蓝牙扫描结束", Toast.LENGTH_SHORT).show()
    }
    }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    fab.setOnClickListener { view ->
    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    .setAction("Action", null).show()
    }
    // 注册广播接收器。
    // 接收蓝牙发现
    val filterFound = IntentFilter(BluetoothDevice.ACTION_FOUND)
    registerReceiver(mReceiver, filterFound)

    val filterStart = IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED)
    registerReceiver(mReceiver, filterStart)

    val filterFinish = IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
    registerReceiver(mReceiver, filterFinish)

    //蓝牙管理,这是系统服务可以通过getSystemService(BLUETOOTH_SERVICE)的方法获取实例
    bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
    //通过蓝牙管理实例获取适配器,然后通过扫描方法(scan)获取设备(device)
    bluetoothAdapter = bluetoothManager.adapter
    if (!bluetoothAdapter.isEnabled) {
    val bluetoothTurnOn = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
    startActivityForResult(bluetoothTurnOn, REQUEST_BLUETOOTH_TURN_ON)
    } else {
    bluetoothAdapter.startDiscovery();
    }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
    REQUEST_BLUETOOTH_TURN_ON->{
    when (resultCode) {
    RESULT_OK->{
    //TextView1.text = "蓝牙开启成功"
    Toast.makeText(this.applicationContext, "蓝牙开启成功", Toast.LENGTH_SHORT).show()
    bluetoothAdapter.startDiscovery()
    }
    RESULT_CANCELED->{
    //TextView1.text = "蓝牙开启失败"
    Toast.makeText(this.applicationContext, "蓝牙开启失败", Toast.LENGTH_SHORT).show()
    }
    }
    }
    }
    }
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)
    return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    return when (item.itemId) {
    R.id.action_settings -> true
    else -> super.onOptionsItemSelected(item)
    }
    }
    }
  • 相关阅读:
    使用用Ghost制作的win2k3和winxp文件具有相同的SID的解决办法
    64 bits Windows 7 使用 regsvr32 的註冊方式(转)
    怎么实现用户匿名访问web,但数据库要用Windows集成验证方式(数据库和web服务器分别在两台机器上)
    为什么按照微软给定的匿名配置Web 同步最终造成创建订阅的步骤总是失败?但改为需要身份验证就行了
    How to edit Team Build Types
    利用WhiteHose一步步建立分布式系统的框架(二)创建LDD步骤
    发现:InfoPath 2007 Training Labs地址
    在MSF中怎么区分易混淆的工作项类型:Bug、风险和问题(我个人的理解)
    RGB Colour Map
    How to distinguish Design time or Running time in Mobile cusotmer Contorl(the NetCF2.0 is different to NetCF1.0)
  • 原文地址:https://www.cnblogs.com/arci/p/8145192.html
Copyright © 2011-2022 走看看