У меня отказывается телефон подключаться к планшету.
Код: Выделить всё
package com.example.bluetoothterminal;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
public BluetoothAdapter btAdapter;
public TextView out;
public EditText MAC_adress;
public Button btn_Connect;
private BluetoothSocket btSocket;
private Object Method;
private static final int REQUEST_ENABLE_BT = 1;
private static String MacAdress = "20:11:02:47:01:60";
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
out = (TextView) findViewById(R.id.textView1);
MAC_adress = (EditText) findViewById(R.id.MAC);
btn_Connect = (Button) findViewById(R.id.button3);
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter != null) {
if (btAdapter.isEnabled()) {
out.setText("Bluetooth включен!");
}else{
Intent enebleBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enebleBtIntent, REQUEST_ENABLE_BT);
out.setText("Bluetooth включен");
}
}else
{
out.setText("Bluetooth не найден");
}
btn_Connect.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
BluetoothDevice device = btAdapter.getRemoteDevice(MAC_adress.getText().toString());
out.setText("Получили Device ="+device.getName());
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btAdapter.cancelDiscovery();
//Toast.makeText(getApplicationContext(), "CancelDiscovery", Toast.LENGTH_LONG).show();
btSocket.connect();
Toast.makeText(getApplicationContext(), "CONNECTED", Toast.LENGTH_LONG).show();
}catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
}
@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);
}
}