|
(二)手机端JAVA程序:
1.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="28dp"
android:text="安卓手机蓝牙控制智能小车"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="18dp"
android:text="重庆邮电大学自动化学院"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp"
android:text="作者:张露"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/textView3"
android:layout_marginBottom="19dp"
android:background="@drawable/backgroundzuo"
android:text="左转" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/backgroundhou"
android:text="后退" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:background="@drawable/backgroundqian"
android:text="前进" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/button1"
android:background="@drawable/backgroundyou"
android:text="右转" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_centerHorizontal="true"
android:text="停止" />
</RelativeLayout>
4.Activity文件
public class Bluetooth_lightActivity extends Activity implements OnClickListener{
String information;
public byte[] message = new byte[1];
private Vibrator vibrator;
private BluetoothAdapter btadapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice btDevice;
BluetoothSocket bluetoothSocket = null;
//BluetoothReceiver mReceiver;
OutputStream tmpOut = null;
private static final UUID MY_UUID_SECURE = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
private String blueAddress="00:12:10:31:01:70";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_light);
if (btadapter != null) {
if (!btadapter.isEnabled()) {
// 通过这个方法来请求打开我们的蓝牙设备
Intent intent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
} else {
System.out.println("本地设备驱动异常!");
}
Button qianjin = (Button) findViewById(R.id.button1);
Button houtui = (Button) findViewById(R.id.button2);
Button zuozhuan = (Button) findViewById(R.id.button3);
Button youzhuan = (Button) findViewById(R.id.button4);
Button tingzhi = (Button) findViewById(R.id.button5);
qianjin.setOnClickListener((OnClickListener) this);
houtui.setOnClickListener((OnClickListener) this);
zuozhuan.setOnClickListener((OnClickListener) this);
youzhuan.setOnClickListener((OnClickListener) this);
tingzhi.setOnClickListener((OnClickListener) this);
}
public void bluesend(byte[] msg) {
try {
tmpOut = bluetoothSocket.getOutputStream();
Log.d("send", Arrays.toString(msg));
tmpOut.write(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void vibrator()
{
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(10);
}
/**
* 按钮点击事件.
* @param v
*/
public void onClick(View v) {
switch (v.getId()) {
//转写按钮
case R.id.button1:
message[0] = (byte) 0x41;
vibrator();
Toast.makeText(this, "前进", Toast.LENGTH_LONG).show();
bluesend(message);
break;
//设置按钮
case R.id.button2:
message[0] = (byte) 0x44;
vibrator();
Toast.makeText(this, "后退", Toast.LENGTH_LONG).show();
bluesend(message);
break;
case R.id.button3:
message[0] = (byte) 0x43;
vibrator();
Toast.makeText(this, "左转", Toast.LENGTH_LONG).show();
bluesend(message);
break;
case R.id.button4:
message[0] = (byte) 0x42;
vibrator();
Toast.makeText(this, "右转", Toast.LENGTH_LONG).show();
bluesend(message);
break;
case R.id.button5:
message[0] = (byte) 0x61;
vibrator();
Toast.makeText(this, "停止", Toast.LENGTH_LONG).show();
bluesend(message);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_bluetooth_light, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
//unregisterReceiver(mReceiver);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// Set<BluetoothDevice> devices = btadapter.getBondedDevices();
btDevice = btadapter.getRemoteDevice(blueAddress);
try {
bluetoothSocket = btDevice
.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
Log.d("ChuyingjihuaActivity", "开始连接...");
bluetoothSocket.connect();
Log.d("ChuyingjihuaActivity", "完成连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|