package blvd.sunset.homesecurity;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import com.android.bluetooth.BluetoothDevices;
import blvd.sunset.homesecurity.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.SensorEventListener;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.util.Log;
//import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
//import android.R;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
int INTENT_LOGIN = 1;
ImageView imgLockDoor;
ImageView imgTurnOnLight;
private final static int MAX_DEVICES = 50;
private final static int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter bluetoothAdapter = null; //系统函数
private BluetoothReceiver bluetoothReceive = null; //这是一个广播接收器
private BluetoothDevices bluetoothDevices[]; //自定义接口
private BluetoothActivity bluetoothActivity;
private final static String BLUETOOTH_MODULE = "F0:6B:CA:35:0F:F0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgLockDoor= (ImageView) findViewById(R.id.img_key);
// imgLockDoor.setOnClickListener(new lockDoorImgListener());
imgLockDoor.setOnClickListener(this);
imgTurnOnLight= (ImageView) findViewById(R.id.img_light);
// imgLockDoor.setOnClickListener(new lockDoorImgListener());
imgLockDoor.setOnClickListener(this);
// bluetooth -=======================================================================================
bluetoothActivity = new BluetoothActivity(this);
bluetoothDevices = new BluetoothDevices[MAX_DEVICES];
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothReceive = new BluetoothReceiver();
registerReceiver(bluetoothReceive,intentFilter);
// If the adapter is null, then Bluetooth is not supported
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
if (!bluetoothAdapter.isEnabled())
{
// enable bluetooth
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
System.out.println("开启设备~~~~~~~~");
// startActivity(enableIntent);
}
bluetoothActivity.setBluetoothAdapter(bluetoothAdapter);
bluetoothActivity.setBluetoothDevices(bluetoothDevices);
System.out.println("已经开启了啊!!!!!!!!!!!!");
// bluetooth -=======================================================================================
}
private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //远程发现的蓝牙
System.out.println(device.getAddress());
//String address=device.getAddress();
if(device.getAddress().equals(BLUETOOTH_MODULE))
{
// 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
bluetoothAdapter.cancelDiscovery();
try {
if (device.getBondState() == BluetoothDevice.BOND_NONE) {
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
Log.e("TAG", "开始配对");
createBondMethod.invoke(device);
} else {
connect(device);
System.out.println("配对了,进行连接吧");
}
} catch (Exception e) {
// TODO: handle exception
// DisplayMessage("无法配对!");
e.printStackTrace();
}
//bluetoothAdapter.cancelDiscovery();
// new ConnectThread(device).start();// 链接
}
else{
System.out.println("地址不一样");
}
}
}
}
private void connect(BluetoothDevice device) throws IOException {
// 固定的UUID
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothSocket bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
//socket.connect();
System.out.println("连接成功");
try { // This is a blocking call and will only return on a successful connection or an exception
bluetoothSocket.connect();
} catch (IOException e1) {
try {
bluetoothSocket.close();
System.out.println("有错误");
} catch (IOException e2) {
System.out.println("有错误22222");
}
}
}
@Override
protected void onDestroy() {
bluetoothActivity.DisconnectFromBluetooth(bluetoothActivity.getBluetoothDeviceIndex());
super.onDestroy();
}
@Override
public void onClick(View v) {
//BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(BLUETOOTH_MODULE);
//bluetoothAdapter.startDiscovery();
//bluetoothActivity.ExpressBT();
//System.out.println("扫面新设备!!!");
bluetoothActivity.ExpressBT();
if (v == imgLockDoor) {
//Modified by Gang 2014-4-8
//bluetoothActivity.show();
//bluetoothActivity.ExpressBT();
}
else if (v == imgTurnOnLight) {
}
}
private class lockDoorImgListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//open the bluetooth
System.out.println("连接去!!!!!!!!!!!!");
//bluetoothActivity.ExpressBT();
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice("FO:6B:CA:35:0F:F0");
/*Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();// 已经配对的蓝牙设备
if(devices.size()>0){
for(Iterator iterator =devices.iterator();iterator.hasNext();){
BluetoothDevice bluetoothDivice =(BluetoothDevice) iterator.next();
System.out.println("已经配对的设备:"+bluetoothDivice.getAddress());
}
}*/
}
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_login) {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivityForResult(i, INTENT_LOGIN);
return true;
}
if (id == R.id.action_bluetooth){
return true;
}
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
android&Bluetooth
最新推荐文章于 2016-07-24 21:26:36 发布