1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| package com.ggelec.unids6;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.*;
public class UniDS6BluetoothActivity extends Activity implements android.view.View.OnClickListener{
Button buttonConnection;
BluetoothAdapter bluetoothAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("", "onCreate");
buttonConnection = ((Button)this.findViewById(R.id.buttonConnection));
buttonConnection.setOnClickListener(this);
}
@Override
protected void onStart() {
Log.i("", "onStart");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(UniDS6BluetoothActivity.this,
"No Bluetooth devices", Toast.LENGTH_LONG).show();
Log.e("ERROR", "No Bluetooth devices");
} else {
Toast.makeText(UniDS6BluetoothActivity.this, "Bluetooth available",
Toast.LENGTH_SHORT).show();
Log.i("INFO", "Bluetooth available");
}
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
}
super.onStart();
}
@Override
protected void onResume() {
Log.i("", "onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.i("", "onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i("", "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i("", "onDestroy");
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.disable();
Log.i("INFO", "Bluetooth disable");
}
super.onDestroy();
}
public void onClick(View v) {
Toast.makeText(UniDS6BluetoothActivity.this, "searching...",
Toast.LENGTH_SHORT).show();
Log.i("INFO", "searching...");
if (bluetoothAdapter.getScanMode() !=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
if(bluetoothAdapter.isDiscovering())bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(UniDS6BluetoothActivity.this, "New Device = " + device.getName(), Toast.LENGTH_SHORT).show();
}
}
};
} |
Partager