google的BluetoothLeGatt例子,修改提取接口之后运行不了
现在的要求是要改些成,点击一个页面的按钮,调用BluetoothLeGatt例子中的函数,把搜索到的设备列表放到ArrayList中
不知道我是怎么写的,改些之后在开启蓝牙这一步就蹦了
//开启蓝牙
mBluetoothAdapter.enable();
google的例子在这里:
http://download.csdn.net/detail/hellogv/7228819
我自己改写的例子的几个文件给贴一下,实在想不出为什么在这里崩了。。
上传不了附件,先贴两个核心的.java:
MainActivity.java:
package com.example.helloworld;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clickHandler(View source) {
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
// Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
// Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
//开启蓝牙
mBluetoothAdapter.enable();
// DeviceScanActivity d = new DeviceScanActivity(mBluetoothAdapter, this);
// d.scanLeDevice(true);
}
}
相关推荐
android-BluetoothLeGatt
Android BluetoothLeGatt示例 此示例演示如何使用蓝牙LE通用属性配置文件(GATT)在设备之间传输任意数据。 介绍 此样本显示了可用的Bluetooth LE设备列表,并提供了一个接口,用于连接,显示数据以及显示设备支持的GATT服务和特性。 它创建了一个用于管理与给定Bluetooth LE设备上托管的GATT服务器的连接和数据通信。 这些活动与服务进行通信,该服务又与交互。 先决条件 Android SDK v23 Android构建工具v23.0.0 Android支持存储库 屏幕截图 入门 本示例使用Gradle构建系统。 要构建此项目,请使用“ gradlew build”命令或在Android Studio中使用“导入项目”。 支持 Google+社区: : 堆栈溢出: : 如果您在此示例中发现错误,请提出问题: : 鼓励使用补丁,
BluetoothLeGatt
Android BluetoothLeGatt示例 此样本演示了如何使用蓝牙LE通用属性配置文件(GATT)在设备之间传输任意数据。 介绍 此样本显示了可用的Bluetooth LE设备列表,并提供了一个接口,用于连接,显示数据以及显示设备支持的GATT服务和特性。 它创建了一个用于管理与给定Bluetooth LE设备上托管的GATT服务器的连接和数据通信。 这些活动与服务进行通信,该服务又与交互。 先决条件 Android SDK v22 Android构建工具v22.0.1 Android支持存储库 屏幕截图 入门 本示例使用Gradle构建系统。 要构建此项目,请使用“ gradlew build”命令或在Android Studio中使用“导入项目”。 支持 Google+社区: : 堆栈溢出: : 如果您在此示例中发现错误,请提出问题: : 鼓励使用补丁
android-bluetoothlegatt-master.zip
Android蓝牙BLE官方demo
谷歌蓝牙示例工程.zip
Google的官方蓝牙演示DEMO源码: (1)BluetoothAdvertisements.zip是Android智能设备作为从设备的DEMO,需要在Android 5.0以上真机运行。 (2)BluetoothLeGatt.zip是Android智能设备作为主设备的DEMO,需要在Android 4.3以上真机运行。 (3)BluetoothChat.zip是一个蓝牙聊天室,两台具备蓝牙的Android智能设备即可安装运行,可以实现经典蓝牙通信方式的数据发送与显示。
android 4.3 ble sample
android 4.3 蓝牙BLE使用例子


