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 ...
随机推荐
- JobTracker作业调度分析
转自:http://blog.csdn.net/Androidlushangderen/article/details/41408517 JobTracker的作业调度给我感觉就是比较宏观意义上的操作 ...
- Web安全测试(一)-手工安全测试方法&修改建议
常见问题 1.XSS(CrossSite Script)跨站脚本攻击 XSS(CrossSite Script)跨站脚本攻击.它指的是恶意攻击者往Web 页面里插入恶意 html代码,当用户浏览该页之 ...
- ajax 跨域 4种方法
一,传统的ajax方法 1,js代码 查看复制打印? $("#ajax").click(function(){ $.ajax({ type: "POST", u ...
- ubuntu安装scala详细教程
ubuntu14 安装scala详细教程 1.下载scala压缩包 http://www.scala-lang.org/download/ 2.建立目录,解压文件到所建立目录 $ sudo mkdir ...
- java web 运动前端
[写在前面的话:]前不久刚看到过一句话:说好的技术文章应该让读者感觉增加信心,而不是失去信心.有感于这句话是因为以前觉得发一些貌似高深的,看起来nb的东西才算一篇好博文,可是多少有点炫技的成分.可是后 ...
- Gabor变换、Gabor滤波器
D.Gabor 1946年提出 窗口Fourier变换,为了由信号的Fourier变换提取局部信息,引入了时间局部化的窗函数. 由于窗口Fourier变换只依赖于部分时间的信号,所以,现在窗口Four ...
- Android中的<include>标签和<merge>标签
android开发中经常会碰到某一个布局的复用:直接拷贝粘贴并不是是有效的策略,这时候就能够借助<include>标签和<merge>标签来完毕. 官方文档: http://d ...
- Python调用外部程序
通过os.system和subprocess.call()函数调用其他程序 预备知识:cmd中打开和关闭程序 cmd中打开程序 a.打开系统自带程序 系统自带的程序的路径一般都已加入环境变量之中,只需 ...
- 【BZOJ4592】[Shoi2015]脑洞治疗仪 线段树
[BZOJ4592][Shoi2015]脑洞治疗仪 Description 曾经发明了自动刷题机的发明家SHTSC又公开了他的新发明:脑洞治疗仪--一种可以治疗他因为发明而日益增大的脑洞的神秘装置. ...
- 高性能Web开发系列
1. 高性能WEB开发基础 http://www.uml.org.cn/net/201404225.asp 2. 高性能WEB开发进阶(上) http://www.uml.org.cn/net/201 ...