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 + "\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)
}
}
}
Android 6.0 Kotlin 蓝牙扫描的更多相关文章
- Android 6.0 Kotlin 蓝牙BLE扫描(改为指定时间没有发现新设备后停止扫描使用interface)
package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...
- Android 6.0 Kotlin 蓝牙BLE扫描
package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...
- Android Studio3.0 Kotlin工程问题集
问题1: 新建支持Kotlin的Android项目,卡在"Resolve dependency :classpath" 解决分析: 一般碰到"Resolve depend ...
- Android网络开发之蓝牙
蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的2.4GHz ISM(I-工业.S-科学.M-医学)频段,其数据速率为1Mbps,采用时分双工传输方案. 蓝牙 ...
- Android开发之蓝牙--扫描已经配对的蓝牙设备
一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) 1.3 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
- 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ A2DP.SINK.sink_connect.s ...
- Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探
Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
关键词:蓝牙blueZ A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...
- Android Studio 蓝牙开发实例——基于Android 6.0
因项目需要做一个Android 的蓝牙app来通过手机蓝牙传输数据以及控制飞行器,在此,我对这段时间里写的蓝牙app的代码进行知识梳理和出现错误的总结. 该应用的Compile Sdk Version ...
随机推荐
- js 控制按钮点击后不可用
<input type="button" id="btn" value="免费获取验证码" /> <script type ...
- C++ 类模板四(typename关键字)
- JAVA8之lambda表达式详解,及stream中的lambda使用
分享文章:https://blog.csdn.net/jinzhencs/article/details/50748202
- C#反射实例(一) 利用反射使用类库
在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,今天把反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,在学好了这一切的基础上,大家可以学习反射的具体 ...
- 【BZOJ】2021: [Usaco2010 Jan]Cheese Towers(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2021 噗,自己太弱想不到. 原来是2次背包. 由于只要有一个大于k的高度的,而且这个必须放在最顶,那 ...
- python urllib2导出elasticsearch数据时 返回 "urllib2.HTTPError: HTTP Error 500: Internal Server Error"
0.业务场景 将ES中某个index的某个字段的所有数据,导出到文件中 1.ES数据导出方法简述 ES数据导出方法,我主要找到了以下几个方面,欢迎大家补充: ES官方API:snapshot and ...
- mysql小技巧记录
1.在mysql场景中,每一行都是用分号(:)作为结束的. 2.在php场景中,最后的分号(:)可以省略掉. 如: <? $sql = "select * from tem" ...
- log4j详解(转)
转载自:http://blog.csdn.net/evatian/article/details/8501517 Log4j – 如何配置多个logger? 分类: java2013-01-14 16 ...
- Objective-C中的instancetype和id关键字(转)
转自:Objective-C中的instancetype和id关键字 一.什么是instancetype 同id一样,都是表示未知类型的的对象. 二.关联返回类型(related result typ ...
- apache 一个站点配置多个域名
<VirtualHost *:80> ServerAdmin i@kuigg.com DocumentRoot /www/kuigg.com ServerName kuigg.com ...