2010年12月7日星期二

Android VideoCamera Analysis

I wanted to know how to use camera recording. So I checked the file "VideoCamera" in android source code. The following was what it did.


1. If you want to start recording, you need Camera and MediaRecorder.
2. Decide when to open a camera device, how to set parameters of the camera and set the previewDisplay always before starting previewing.
3. Decide when to close a camera device.
4. Stop the camera and media recording in onPause() and resume them in onResume().
5. in callback method surfaceCreated, mSurfaceHolder is set.
6. in callback method surfaceDestroy, mSurfaceHolder is destroyed.
7. in callback method surfaceChanged, mSurfaceHolder is set again. But if the state of the Activity is Pausing, do nothing just return. Restart previewing and recording.


The following is an example.

vim+ctags+cscope+java

How to let cscope to support JAVA?
1. find . -name "*.java" >> cscope.files
2. cscope -b  (Refresh the database)
3. cscope

2010年12月5日星期日

Activity orientation | Activity Restart

The values of Orientation are ORIENTATION_PORTRAIT and
ORIENTATION_LANDSCAPE.


When orientation is changed, the default action is to restart the Activtiy.

If you don’t want your Activity to restart, you should do the following thing:


Config your Activity with the Activity Element
     android:configChanges="keyboardHidden|orientation"
Rewrite the callback method
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (this.getResources().getConfiguration().orientation ==
    Configuration.ORIENTATION_LANDSCAPE) {
    Log.d(TAG, "++ ORIENTATION_LANDSCAPE ++");
   
    } else if (this.getResources().getConfiguration().orientation
    == Configuration.ORIENTATION_PORTRAIT){
    Log.d(TAG, "++ ORIENTATION_PORTRAIT ++");
    }
}

2010年12月2日星期四

Android Bluetooth

Android Bluetooth

Bluetooth is a communication protocol designed for short-range, low-bandwidth, peer-to-peer communications.
Bluetooth devices and connections are only managed by the following classes:
l         BluetoothAdapter local Bluetooth adapter
l         BluetoothDevice remote Bluetooth device
l         BluetoothSocket
l         BluetoothServerSocket

Accessing the local BT adapter

BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();
In order to access the local BT adapter, you should include the Bluetooth manifest permission.
"android.permission.BLUETOOTH" />
"android.permission.BLUETOOTH_ADMIN" />

Managing BT Properties and State

The most useful properties are friendly name and MAC address:
mBTAdapter.getName()
mBTAdapter.getAddress()
The current BT Adpater’s state:
l         STATE_TURNING_ON
l         STATE_ON
l         STATE_TURNING_OFF
l         STATE_OFF

Turning on local BT Adapter

private static final int RQUEST_ENABLE_BT = 0;
Intent enalbeIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, RQUEST_ENABLE_BT);
A sub-activity will be pop-up. The result code of the sub-activity will be returned on the callback method onActivityResult(int requestCode, int resultCode, Intent data).

Being discoverable

The BT Discoverability is indicated by the scan mode.
There are three scan modes (getScanMode()).
SCAN_MODE_CONNECTABLE_DISCOVERABLE
SCAN_MODE_CONNECTABLE
SCAN_MODE_NONE
To turn on discovery you need to obtain permission from the user by starting an Activity:
String dis = BluetoothAdapter.ACTION_RQUEST_DISCOVERABLE;
Intent mDis = new Intent(dis);
StartActivityForResult(mDis, DISCOVERY_REQUEST);
To start discovery process, you should call function
mAdapter.startDiscovery();
To cancel discovery process, you should call function
mAdapter.cancelDiscovery();
To get the remote bluetooth device discovered, you should use the BroadcastReceiver,
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
BroadcastReceiver discoveryResult = new BroadcastReceiver(){
public void onReceiver(Context ct, Intent intent){
   String mRemoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
   BluetoothDevice mRemoteDevice;
   mRemoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
}
}

Bluetooth Communications

The communication channel is a RFCOMM channel.
To establish the bidirectional communications, you should use the following classes:
l         BluetoothServerSocket  A bluetooth server socket is used to listen for incoming Bluetooth Socket connection request from remote Bluetooth Devices.
l         BluetoothSocket
The server socket is created by listenUsingRfcommWithServiceRecord method on mAdapter. The server socket is identified by a name and a UUID.
To start listening for connections call accept on the server.

Transmitting Data Using Bluetooth Sockets

Once a channel is established, the two peers can talk with each other by BluetoothSocket.