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 蓝牙扫描的更多相关文章

  1. Android 6.0 Kotlin 蓝牙BLE扫描(改为指定时间没有发现新设备后停止扫描使用interface)

    package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...

  2. Android 6.0 Kotlin 蓝牙BLE扫描

    package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...

  3. Android Studio3.0 Kotlin工程问题集

    问题1: 新建支持Kotlin的Android项目,卡在"Resolve dependency :classpath" 解决分析: 一般碰到"Resolve depend ...

  4. Android网络开发之蓝牙

    蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的2.4GHz ISM(I-工业.S-科学.M-医学)频段,其数据速率为1Mbps,采用时分双工传输方案.   蓝牙 ...

  5. Android开发之蓝牙--扫描已经配对的蓝牙设备

    一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等. 二. ...

  6. 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ  A2DP.SINK.sink_connect.s ...

  7. Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探

    Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...

  8. Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    关键词:蓝牙blueZ  A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...

  9. Android Studio 蓝牙开发实例——基于Android 6.0

    因项目需要做一个Android 的蓝牙app来通过手机蓝牙传输数据以及控制飞行器,在此,我对这段时间里写的蓝牙app的代码进行知识梳理和出现错误的总结. 该应用的Compile Sdk Version ...

随机推荐

  1. 如何给RecyclerView加上滚动条--现在就教你

    时隔许久,我又要更博了,含蓄的话不多说了,今天我们的主题是这个RecyclerView.至于为什么要加个scrollBar?因为我的业务需求是需要一个实现成这样的, 效果图:(可能看起来比较粗糙,但功 ...

  2. 第一百三十九节,JavaScript,封装库--CSS选择器

    JavaScript,封装库--修改元素选择器 就是将构造库函数修改成元素选择器,像css那样,输入#xxx .xxx xxx  (获取指定id下的指定class下的指定标签元素) 修改后的基础库 / ...

  3. 日期类Date

    Java在日期类中封装了有关日期和时间的信息,用户可以通过调用相应的方法来获取系统时间或设置日期和时间.Date类中有很多方法在JDK1.0公布后已经过时了,在8.3中我们将介绍JDK1.0中新加的用 ...

  4. (转载)【C#4.0】dynamic和var及object

    dynamic a = 10;a = a + 10;Console.WriteLine(a.GetType()); 此段代码会输出 System.Int32,第二行不需要类型转换,因为在运行时识别类型 ...

  5. (转)java fail-fast机制

    转自:http://blog.csdn.net/chenssy/article/details/38151189 Java提高篇(三四)-----fail-fast机制 标签: javajava提高篇 ...

  6. 三角矩阵怎么用MathType输入

    虽然现在已经是暑假,但还是有很多学霸们在炎炎夏日中努力奋战,连暑假都不放过.也许正在实验室里面做得昏天暗地,也许是正在跟数据努力奋战,也许还在办公室里面一点一点地码着论文.码论文的时候,不时地要敲着复 ...

  7. iOS-如何在项目中使用CocoaPods

    1.打开终端. 2.将项目文件夹拖入到cd空格 后面. 3.回车,看下现在的路径是. 4.我们输入vim podfile回车,建立Podfile文件. 5.进到这个界面,我们再按i编辑--你会看到下面 ...

  8. IOS 预览pdf,word文档的集中方式

    在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...

  9. location.assign 与 location.replace的区别

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  10. 开源平台ghost博客系统

    http://docs.ghost.org/installation/windows/ 不会安装的童鞋可以到这里论坛看看 此教程只说windows下的安装: mac的安装教程请移步这里 mac版安装 ...