package com.arci.myapplicationimport android.app.Activityimport android.os.Bundleimport android.support.design.widget.Snackbarimport android.support.v7.app.AppCompatActivityimport android.view.Menuimport android.view.MenuItemimport android.view.Viewimport kotlinx.android.synthetic.main.activity_main.*import android.widget.TextViewimport kotlinx.android.synthetic.main.content_main.*import android.bluetooth.BluetoothManagerimport android.content.Contextimport android.bluetooth.BluetoothAdapterimport android.widget.Toastimport android.content.Intentimport android.text.method.TextKeyListener.clearimport android.bluetooth.BluetoothDeviceimport android.bluetooth.le.BluetoothLeScannerimport android.content.BroadcastReceiverimport android.content.IntentFilterclass 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 + "\n設備地址:" + 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) } }}